Clock tickSeconds() method in Java


The current ticking with the system clock in seconds can be obtained using the method tickSeconds() in the Clock Class in Java. This method requires a single parameter i.e. the time zone and it returns the current ticking value in seconds.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      ZoneId zone = ZoneId.of("Australia/Melbourne");
      Clock c = Clock.tickSeconds(zone);
      System.out.println("The current ticking value in seconds is: " + c.instant());
   }
}

Output

The current ticking value in seconds is: 2019-02-07T05:55:23Z

Now let us understand the above program.

The current ticking value in seconds is obtained using the method tickSeconds(). Then the method instant() is used to print the instance of the clock. A code snippet that demonstrates this is as follows −

ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c = Clock.tickSeconds(zone);
System.out.println("The current ticking value in seconds is: " + c.instant());

Updated on: 30-Jul-2019

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements