Clock system() Method in Java


The current instance of the clock for the required zoneID can be obtained using the method system() in Clock Class in Java. This method requires a single parameter i.e. the zoneID or the time zone and it returns the current instance of the clock for that time zone.

A program that demonstrates this is given as follows −

Example

import java.time.*;
public class Main {
   public static void main(String[] args) {
      ZoneId zone = ZoneId.of("Australia/Melbourne");
      Clock c = Clock.system(zone);
      ZonedDateTime zdt = c.instant().atZone(c.getZone());
      System.out.println("The Date Time for the given zone is: " + zdt.toString());
   }
}

Output

The Date Time for the given zone is: 2019-02-06T23:03:16.395+11:00[Australia/Melbourne]

Now let us understand the above program.

The current instance of the clock for the required zoneID is obtained using the method system(). The instant is displayed using the method toString(). A code snippet that demonstrates this is as follows −

ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c = Clock.system(zone);
ZonedDateTime zdt = c.instant().atZone(c.getZone());
System.out.println("The Date Time for the given zone is: " + zdt.toString());
raja
Published on 13-Mar-2019 10:37:17
Advertisements