Clock systemDefaultZone() Method in Java with Examples
java.time.Clock.systemDefaultZone() 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 where Zone of the returned clock is default time-zone.
This method can use System.currentTimeMillis(), or other higher resolution clock for its implementation if the clock is available to use. This method is recommended to use when the current instant is needed without the date or time. But if operation of date and time is needed, then system() method must be used. This method is similar as system(ZoneId.systemDefault()). Returned clock from this method is immutable, thread-safe and Serializable.
Syntax:
public static Clock systemDefaultZone()
Returns: This method returns a clock that uses the best available system clock in the default zone
Example:
Code: //Clock with default zone Clock clock=Clock.systemDefaultZone(); System.out.println(clock.instant()); Output:: 2018-08-21T10:25:52.361Z Explanation:: when you call systemDefaultZone() for Clock then the systemDefaultZone() method will return a Class Object whose Zone is default Time Zone.
Below programs illustrates systemDefaultZone() method of java.time.Clock class:
Program 1: When Clock is created with systemDefaultZone().
This method makes clock zone to Default Zone. Below program print date and time of clock in ZonedDateTime format.
// Java program to demonstrate // systemDefaultZone() method of Clock class import java.time.*; // create class public class systemDefaultZoneMethodDemo { // Main method public static void main(String[] args) { // create Clock with systemDefaultZone() method Clock clock = Clock.systemDefaultZone(); // 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 ZonedDateTime System.out.println("ZonedDateTime of class" + " with default Zone is " + time.toString()); } } |
ZonedDateTime of class with default Zone is 2018-08-22T11:34:36.510Z[Etc/UTC]
Program 2: Print the zoneId using getZone() for clock created by systemDefaultZone().
// Java program to demonstrate // systemDefaultZone() method of Clock class import java.time.*; // create class public class systemDefaultZoneMethodDemo { // Main method public static void main(String[] args) { // create Clock with systemDefaultZone() method Clock clock = Clock.systemDefaultZone(); // get ZoneId of Clock ZoneId zone = clock.getZone(); // print details of ZoneId of new Clock System.out.println("ZoneID of class is " + zone); } } |
ZoneID of class is Etc/UTC
Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#systemDefaultZone–
Recommended Posts:
- Clock tickSeconds() method in Java with Examples
- Clock tick() Method in Java with Examples
- Java 8 Clock instant() method with Examples
- Java 8 Clock millis() Method with Examples
- Java 8 Clock getZone() method with Examples
- Java 8 Clock hashCode() method with Examples
- Java 8 Clock offset() method with Examples
- Java 8 Clock fixed() method with Examples
- Java 8 Clock equals() Method with Examples
- Clock systemUTC() Method in Java with Examples
- Clock system() Method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Android | How to display Analog clock and Digital clock
- Java Applet | How to display a Digital Clock
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.



