Clock systemDefaultZone() Method in Java

Java 8ProgrammingObject Oriented Programming

The current instance of the clock with the default time zone can be obtained using the method systemDefaultZone() in the Clock Class in Java. This method requires no parameters and it returns the current instance of the clock with the default time zone.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Clock c = Clock.systemDefaultZone();
      Instant i = c.instant();
      ZonedDateTime zdt = i.atZone(c.getZone());
      System.out.println(zdt.toString());
   }
}

Output

2019-02-07T07:55:24.162Z[Etc/UTC]

Now let us understand the above program.

The current instance of the clock with the default time zone is obtained using the method systemDefaultZone(). Then the ZonedDateTime is printed using the method toString(). A code snippet that demonstrates this is as follows −

Clock c = Clock.systemDefaultZone();
Instant i = c.instant();
ZonedDateTime zdt = i.atZone(c.getZone());
System.out.println(zdt.toString());
raja
Published on 13-Mar-2019 15:27:33
Advertisements