Saturday, September 6, 2025
HomeLanguagesJavaClock tickSeconds() method in Java with Examples

Clock tickSeconds() method in Java with Examples

java.time.Clock.tickSeconds(ZoneId zone) method is a static method of Clock class that returns a clock that returns the current instant ticking in whole Seconds using the best available system clock and zone of instant is same as the instant passed as a parameter.

The returned clock is also immutable, thread-safe, and Serializable and this method is equivalent to tick(system(zone), Duration.ofSeconds(1)).

Syntax:

public static Clock tickSeconds(ZoneId zone)

Parameters: This method takes a mandatory parameter zone, which is the time-zone to use to round off instant of a clock in the whole Second.

Return Value: This method returns a clock that returns the current instant ticking in whole Second with zone same as zone passed as parameter.

Example:

Code:
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
Clock clock = Clock.tickSeconds(zoneId);
System.out.println(clock.instant());

Output:
2018-08-21T20:22:32Z

Explanation::
method tickSeconds() returns the instant 
which ticks in a whole Second means 
nanosecond field is zero.

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

Program 1; When Clock is created with Zone Kolkata and printing instant of the clock ticking in whole Second.




// Java program to demonstrate
// tickSeconds() method of Clock class
  
import java.time.*;
  
// create class
public class tickSecondsMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
        // Zone Id with Zone Asia/Calcutta
        ZoneId zoneId = ZoneId.of("Asia/Calcutta");
  
        // create a clock which ticks in the whole Second
        Clock clock = Clock.tickSeconds(zoneId);
  
        // print instance of clock
        System.out.println(clock.instant());
    }
}


Output:

2018-08-22T11:27:38Z

Program 2: Print the date and Time of clock with zone Europe/Paris and clock ticks per whole Second.




// Java program demonstrate
// tickSeconds() method of Clock class
  
import java.time.*;
  
// create class
public class tickSecondsMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // Zone Id with Zone Europe/Paris
        ZoneId zoneId = ZoneId.of("Europe/Paris");
  
        // create a clock which ticks in the whole Second
        Clock clock = Clock.tickSeconds(zoneId);
  
        // get ZonedDateTime object to print time
        ZonedDateTime time = clock
                                 .instant()
                                 .atZone(clock.getZone());
  
        // print time variable value
        System.out.println("Date and Time :" + time);
    }
}


Output:

Date and Time :2018-08-22T13:27:41+02:00[Europe/Paris]

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#tickSeconds-java.time.ZoneId-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS