Clock systemUTC() Method in Java with Examples

java.time.Clock.systemUTC() method is a static method of Clock class which returns a clock that returns the current instant of the clock using the best available system clock where Zone of the returned clock is UTC time-zone.

When the current instant is needed without the date or time, then use systemUTC() method in place of systemDefaultZone().

When converting instant to date and time, then the converter uses UTC time-zone as Zone for conversion. This clock is based on the best available system clock. This method can use System.currentTimeMillis(), or other higher resolution clock for its implementation if the clock is available to use.

Returned clock from this method is immutable, thread-safe and Serializable.

Syntax:



public static Clock systemUTC()

Return Value: This method returns a clock that uses the best available system clock in the UTC time-zone

Example:

Code:
//Clock with default zone
Clock clock=Clock.systemUTC();
System.out.println(clock.instant());

Output:: 
2018-08-21T20:38:10.772Z

Explanation:: 
when you call systemUTC() for Clock then the systemUTC()
method will return a Class Object whose Zone is UTC time zone.

Below programs illustrates systemUTC() method of java.time.Clock class:

Program 1: When Clock is created with systemUTC().

This method makes clock zone to UTC Zone.Below program print date and time of clock in ZonedDateTime format.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// systemUTC() method of Clock class
  
import java.time.*;
  
// create class
public class systemUTCMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create Clock with systemUTC() method
        Clock clock = Clock.systemUTC();
  
        // 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 UTC"
                           + " Time Zone is "
                           + time.toString());
    }
}

chevron_right


Output:

ZonedDateTime of class with UTC Time Zone is 2018-08-22T11:41:15.554Z

Program 2: Print the zoneId using getZone() for clock created by systemUTC().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// systemUTC() method of Clock class
  
import java.time.*;
  
// create class
public class systemUTCMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create Clock with systemUTC() method
        Clock clock = Clock.systemUTC();
  
        // get ZoneId of Clock
        ZoneId zone = clock.getZone();
  
        // print details of ZoneId of new Clock
        System.out.println("ZoneID of class is " + zone);
    }
}

chevron_right


Output:

ZoneID of class is Z

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#systemUTC–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.