Clock system() Method in Java with Examples
java.time.Clock.system(ZoneId zone) method is a static method of Clock class which returns a clock that returns the current instant of the clock using best available system clock with ZoneID of the returned clock is set to the ZoneID passed. This method can use System.currentTimeMillis(), or other higher resolution clock if the clock is available to use.
At the time of conversion from instant to date or time, the specified time-zone is used to give date and time of that timezone. Returned clock from this method is immutable, thread-safe and Serializable.
Syntax:
public static Clock system(ZoneId zone)
Parameters: This method takes a mandatory parameter zone which is the time-zone to use at time of conversion of the instant to date-time
Returns: The method returns Clock object for the given ZoneId
Example:
Code:
// create a Zone Id for Europe/Paris
ZoneId zoneId = ZoneId.of("Europe/Paris");
// base Clock with default zone
Clock realClock=Clock.system(zoneId);
System.out.println(clock.instant());
Output::
2018-08-21T10:25:52.361Z
Explanation::
when you call system(ZoneId) for Clock then the system(ZoneId)
method will return a Class Object for the given ZoneId.you can get
date and time of clock by using instant of class.
Below programs illustrates system(ZoneId) method of java.time.Clock class:
Program 1: When Clock is created with system(ZoneId) where ZoneId is “Europe/Paris” and print date and time of clock.
// Java program to demonstrate // system(ZoneId) method of Clock class import java.time.*; // create class public class systemMethodDemo { // Main method public static void main(String[] args) { // create a Zone Id for Europe/Paris ZoneId zoneId = ZoneId.of("Europe/Paris"); // create Clock with system(zoneId) method Clock clock = Clock.system(zoneId); // get instant of class Instant instant = clock.instant(); // get ZonedDateTime object from instantObj to get date time ZonedDateTime time = instant.atZone(clock.getZone()); // print details of time System.out.println("Instant for class is " + time.toString()); } } |
Instant for class is 2018-08-22T13:53:35.779+02:00[Europe/Paris]
Program 2: Create Clock with Zone “US/Arizona” using system() and print the zoneId using getZone().
// Java program to demonstrate // system(ZoneId) method of Clock class import java.time.*; // create class public class systemMethodDemo { // Main method public static void main(String[] args) { // create a Zone Id for US/Arizona ZoneId zoneId = ZoneId.of("US/Arizona"); // create Clock with system(zoneId) method Clock clock = Clock.system(zoneId); // print details of ZoneId of new Clock System.out.println("ZoneID of class is " + clock.getZone()); } } |
ZoneID of class is US/Arizona
Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#system-java.time.ZoneId-
Recommended Posts:
- Java 8 Clock fixed() method with Examples
- Java 8 Clock getZone() method with Examples
- Java 8 Clock hashCode() method with Examples
- Java 8 Clock instant() method with Examples
- Java 8 Clock millis() Method with Examples
- Java 8 Clock equals() Method with Examples
- Java 8 Clock offset() method with Examples
- Clock tick() Method in Java with Examples
- Clock systemDefaultZone() Method in Java with Examples
- Clock tickSeconds() method in Java with Examples
- Clock systemUTC() Method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- System.lineSeparator() method in Java With Examples
- System.identityHashCode() Method in Java With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



