OffsetTime now() method in Java with examples

  1. The now() method of OffsetTime class in Java obtains the current time from the system clock in the default time-zone.

    Syntax :

    public static OffsetTime now()
    

    Parameter: This method does not accepts any parameter.

    Return Value: It returns the current time using the system clock and default time-zone and not null.



    Below programs illustrate the now() method:

    Program 1 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the now() method
      
    import java.time.OffsetTime;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Parses the time
            OffsetTime time = OffsetTime.now();
      
            // Prints the current time
            System.out.println("Current time: " + time);
        }
    }

    chevron_right

    
    

    Output:

    Current time: 02:58:01.700Z
    
  2. The now(clock) method of OffsetTime class in Java obtains the current time from the specified clock in the parameter.

    Syntax :

    public static OffsetTime now(Clock clock)
    

    Parameter: This method accepts a single parameter clock which specifies the clock to use and not null.

    Return Value: It returns the current time and not null.

    Below programs illustrate the now(clock) method:

    Program 1 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the now(clock) method
      
    import java.time.OffsetTime;
    import java.time.Clock;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Parses the time
            OffsetTime time = OffsetTime.now();
      
            // Prints the current time
            System.out.println("Current time: " + Clock.systemUTC());
        }
    }

    chevron_right

    
    

    Output:

    Current time: SystemClock[Z]
    
  3. The now(zone) method of OffsetTime class in Java obtains the current time from the system clock in the specified time-zone in the parameter.

    Syntax :


    public static OffsetTime now(ZoneId zone)
    

    Parameter: This method accepts a single parameter zone which specifies the zone ID to use, not null.

    Return Value: It returns the current time and not null.

    Below programs illustrate the now(clock) method:

    Program 1 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the now(clock) method
      
    import java.time.OffsetTime;
    import java.time.ZoneId;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Parses the time
            OffsetTime time = OffsetTime.now();
      
            // Prints the current time
            System.out.println("Current time: " + ZoneId.systemDefault());
        }
    }

    chevron_right

    
    

    Output:

    Current time: Etc/UTC
    

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#now-long-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai