Clock systemDefaultZone() Method in Java


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements