Java 8 Clock getZone() method

Java 8ProgrammingObject Oriented Programming

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);
raja
Published on 13-Mar-2019 15:45:58
Advertisements