Java 8 Clock getZone() method


The time zone required for the date and time creation can be obtained using the method getZone() in the Clock Class in Java. This method requires no parameters and it returns the required 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();
      ZoneId z = c.getZone();
      System.out.println("The clock is: " + c);
      System.out.println("The ZoneId is: " + z);
   }
}

Output

The clock is: SystemClock[Etc/UTC]
The ZoneId is: Etc/UTC

Now let us understand the above program.

The time zone is obtained using the method getZone() and then this value is displayed. A code snippet that demonstrates this is as follows −

Clock c = Clock.systemDefaultZone();
ZoneId z = c.getZone();
System.out.println("The clock is: " + c);
System.out.println("The ZoneId is: " + z);

Updated on: 30-Jul-2019

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements