Java Programming Tutorials

Java programming tutorials with many code examples!

Java Time Clock tickSeconds(ZoneId)

java.time.Clock.tickSeconds(ZoneId)

The method Clock.tickSeconds(ZoneId zone) returns a clock that will tick in seconds using the best available clock (usually system clock).

Method signature

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

public static Clock tickSeconds(ZoneId zone)

The method is available since Java 8.

Parameters

  • ZoneId zone: the time-zone that will be used to convert the instant to date-time, not null.

Return value

Returns a clock that ticks in seconds using the given time-zone, never null.

Exceptions

  • NullPointerException: if the zone is null.

Example usage

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

package com.farenda.java.time;

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

public class ClockExample {

    public static void main(String[] args) throws InterruptedException {
        // Clocks:
        Clock sysClock = Clock.systemDefaultZone();
        Clock laClock = Clock.tickSeconds(
                ZoneId.of("America/Los_Angeles"));
        Clock kaClock = Clock.tickSeconds(
                ZoneId.of("Europe/Kaliningrad"));

        // millis from the start of the epoch:
        System.out.println("Sys clock millis: "
                + sysClock.millis());
        System.out.println("LA clock millis : "
                + laClock.millis());
        System.out.println("KA clock millis : "
                + kaClock.millis());

        // date time with zone:
        System.out.println("System clock time  : "
                + ZonedDateTime.now(sysClock));
        System.out.println("LA time tickSeconds: "
                + ZonedDateTime.now(laClock));
        System.out.println("KA time tickSeconds: "
                + ZonedDateTime.now(kaClock));
    }
}

The above code produces the following output:

Sys clock millis: 1464950461956
LA clock millis : 1464950461000
KA clock millis : 1464950461000
System clock time  : 2016-06-03T12:41:01.957+02:00[Europe/Warsaw]
LA time tickSeconds: 2016-06-03T03:41:02-07:00[America/Los_Angeles]
KA time tickSeconds: 2016-06-03T12:41:02+02:00[Europe/Kaliningrad]
Share with the World!