Open In App

OffsetTime now() method in Java with examples

Last Updated : 30 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report
  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 :




    // 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);
        }
    }

    
    

    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 :




    // 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());
        }
    }

    
    

    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 :




    // 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());
        }
    }

    
    

    Output:

    Current time: Etc/UTC
    

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads