Open In App

LocalTime of() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report
  1. The of(int hour, int minute) method of the LocalTime class in Java is used to create an instance of LocalTime from the passed values of hour and minute. In this method, the hour and minute are passed in the Integer format and it returns time-based on these values. The values of second and nanosecond are set to zero by default in this method.

    Syntax:

    public static LocalTime of(int hour,
                               int minute)
    

    Parameters: This method accepts two parameters:

    • hour – It is of Integer type and represents the hour of the day. It varies from 0 to 23.
    • minute – It is of Integer type and represents the minute of the hour. It varies from 0 to 59.

    Return value: This method returns the LocalTime.

    Exception: This method throws DateTimeException if any parameter value either of hour or minute exceeds the range.

    Below program illustrates the of(hour, minute) method of LocalTime in Java:

    Program:




    // Java program to demonstrate
    // LocalTime of(int hour,
    // int minute) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create LocalTime object
            LocalTime localtime
                = LocalTime.of(6, 5);
      
            // Print time
            System.out.println("TIME: "
                               + localtime);
        }
    }

    
    

    Output:

    TIME: 06:05
    
  2. The of(int hour, int minute, int second) method of the LocalTime class in Java is used to create an instance of LocalTime from the passed values of hour, minute and second. In this method, the values of the hour, minute and second are passed in an Integer format and it returns the time on the basis of the passed values. The value of the nanosecond is set to zero by default in this method.

    Syntax:

    public static LocalTime of(int hour,
                               int minute,
                               int second)
    

    Parameters: This method accepts three parameters:

    • hour – It is of Integer type and represents the hour of the day. It varies from 0 to 23.
    • minute – It is of Integer type and represents the minute of the hour. It varies from 0 to 59.
    • second – It is of Integer type and represents the second of the minute. It varies from 0 to 59.

    Return value: This method returns the LocalTime.

    Exception: This method throws DateTimeException if any parameter exceeds the range.

    Below program illustrates the of(hour, minute, second) method of LocalTime in Java:

    Program:




    // Java program to demonstrate
    // LocalTime of(int hour, int minute, int second) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create LocalTime object
            LocalTime localtime
                = LocalTime.of(6, 5, 40);
      
            // Print time
            System.out.println("TIME: "
                               + localtime);
        }
    }

    
    

    Output:

    TIME: 06:05:40
    
  3. The of(int hour, int minute, int second, int nanosecond) method of the LocalTime class in Java is used to create an instance of LocalTime from given values (passed values) of hour, minute, second and nanosecond. In this method, the values of the hour, minute, second and nanosecond os passed in an Integer form and it returns time on the basis of these values. No parameter value is set to zero in this method.

    Syntax:

    public static LocalTime of(int hour,
                               int minute,
                               int second,
                               int nanosecond)
    

    Parameters: This method accepts four parameters:

    • hour – It is of Integer type and represents the hour of the day. It varies from 0 to 23.
    • minute – It is of Integer type and represents the minute of the hour. It varies from 0 to 59.
    • second – It is of Integer type and represents the second of the minute. It varies from 0 to 59.
    • nanosecond – It is of Integer type and represents the nano of the second. It varies from 0 to 999999999.

    Return value: The method returns the LocalTime.

    Exception: The method throws DateTimeException if any parameter value either of hour, minute, second or nanosecond exceeds the range.

    Below program illustrates the of(hour, minute, second, nanosecond) method of LocalTime in Java:

    Program:




    // Java program to demonstrate
    // LocalTime of(int hour, int minute,
    // int second, int nanosecond) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create LocalTime object
            LocalTime localtime
                = LocalTime.of(
                    6, 5, 40, 50);
      
            // Print time
            System.out.println("TIME: "
                               + localtime);
        }
    }

    
    

    Output:

    TIME: 06:05:40.000000050
    

References:



Last Updated : 05 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads