Java Programming Tutorials

Java programming tutorials with many code examples!

Java Time Clock systemUTC

java.time.Clock.systemUTC()

The method Clock.systemUTC() returns a clock that will use the best available system clock to convert instant into date and time using UTC time-zone.

Method signature

The signature of the java.time.Clock.systemUTC() method is as follows:

public static Clock systemUTC()

The method is available since Java 8.

Parameters

The method takes no parameters.

Return value

Returns a clock that is using the best available clock in the UTC time zone, never null.

Exceptions

The method throws no exceptions.

Example usage

In the following code we use java.time.Clock.systemUTC():

package com.farenda.java.time;

import java.time.Clock;
import java.time.ZoneId;

public class ClockExample {

    public static void main(String[] args) {
        Clock utcClock = Clock.systemUTC();
        System.out.println("Clock's zone: "
                + utcClock.getZone());
        System.out.println("UTC Clock millis:         "
                + utcClock.millis());

        System.out.println("System.currentTimeMillis: "
                + System.currentTimeMillis());

        // It's the same as:
        Clock sysClock = Clock.system(ZoneId.of("UTC"));
        System.out.println("System clock's zone: "
                + sysClock.getZone());
        System.out.println("System Clock millis:      "
                + sysClock.millis());

        // In other zone:
        sysClock = Clock.system(ZoneId.of("Europe/Warsaw"));
        System.out.println("System clock's zone: "
                + sysClock.getZone());
        System.out.println("System Clock millis:      "
                + sysClock.millis());
    }
}

The above code produces the following output:

Clock's zone: Z
UTC Clock millis:         1464341174717
System.currentTimeMillis: 1464341174717
System clock's zone: UTC
System Clock millis:      1464341174719
System clock's zone: Europe/Warsaw
System Clock millis:      1464341175086

References:

Share with the World!