Java Clock tick Method
May 14, 2019
Clock.tick
obtains a clock that returns instants from the specified clock truncated to the nearest occurrence of the specified duration. The truncated instant will be lesser than the actual instant close to specified duration and the minimum time unit will be the specified time unit. The obtained clock will only tick as per the specified duration. Find the method signature of tick
from Java doc.
public static Clock tick(Clock baseClock, Duration tickDuration)
baseClock
is the base clock to base the ticking clock on. The tickDuration
is the duration of each visible tick. Tick duration cannot be negative or null.
For 1 millisecond duration we can use
tickMillis
and for 1 second duration we can use tickSeconds
and for 1 minute duration we can use tickMinutes
method of Clock
.
Contents
Duration in Milliseconds
When duration is passed 100 milliseconds totick
method, the instant()
method of Clock
will give current instant truncated to the nearest occurrence of 100 milliseconds duration. The truncated instant will always be lesser than base instant.
DurationInMillis.java
package com.concretepage; import java.time.Clock; import java.time.Duration; public class DurationInMillis { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); Clock tickClock = Clock.tick(baseClock, Duration.ofMillis((100))); for (int i = 1; i <= 3; i++) { System.out.println("-----" + i + "-----"); System.out.println("Base: " + baseClock.instant()); System.out.println("Tick: " + tickClock.instant()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
-----1----- Base: 2019-05-14T06:53:47.770722800Z Tick: 2019-05-14T06:53:47.700Z -----2----- Base: 2019-05-14T06:53:49.798726400Z Tick: 2019-05-14T06:53:49.700Z -----3----- Base: 2019-05-14T06:53:51.812130Z Tick: 2019-05-14T06:53:51.800Z
tick
gives instant truncated to 100 milliseconds duration i.e. 700 milliseconds. The minimum time unit will be millisecond. The next tick of clock will take place only after 100 milliseconds and until then instant will not change.
Suppose we pass 1 millisecond duration.
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofMillis(1));
tickMillis
method instead.
Clock clock = Clock.tickMillis(zoneId);
Duration in Seconds
When the duration is passed 10 seconds totick
method, the clock will give current instant truncated to the nearest occurrence of 10 second duration and the minimum time unit will be second. We will get truncated instant lesser than base instant. The next tick of clock will take place only after 10 seconds and until then instant will not change.
DurationInSeconds.java
package com.concretepage; import java.time.Clock; import java.time.Duration; public class DurationInSeconds { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); Clock tickClock = Clock.tick(baseClock, Duration.ofSeconds((10))); for (int i = 1; i <= 3; i++) { System.out.println("-----" + i + "-----"); System.out.println("Base: " + baseClock.instant()); System.out.println("Tick: " + tickClock.instant()); try { Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
-----1----- Base: 2019-05-14T08:31:20.637866200Z Tick: 2019-05-14T08:31:20Z -----2----- Base: 2019-05-14T08:31:28.656324800Z Tick: 2019-05-14T08:31:20Z -----3----- Base: 2019-05-14T08:31:36.656782400Z Tick: 2019-05-14T08:31:30Z
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofSeconds(1));
tickSeconds
method instead.
Clock clock = Clock.tickSeconds(zoneId);
Duration in Minutes
When the duration is passed 5 minutes totick
method, the clock will give current instant truncated to the nearest occurrence of 5 minutes duration and the minimum time unit will be minute. We will get truncated instant lesser than base instant. The next tick of clock will take place only after 5 minutes and until then instant will not change.
DurationInMinutes.java
package com.concretepage; import java.time.Clock; import java.time.Duration; public class DurationInMinutes { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); Clock tickClock = Clock.tick(baseClock, Duration.ofMinutes((5))); for (int i = 1; i <= 3; i++) { System.out.println("-----" + i + "-----"); System.out.println("Base: " + baseClock.instant()); System.out.println("Tick: " + tickClock.instant()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
-----1----- Base: 2019-05-14T08:37:34.629257200Z Tick: 2019-05-14T08:35:00Z -----2----- Base: 2019-05-14T08:37:36.651372900Z Tick: 2019-05-14T08:35:00Z -----3----- Base: 2019-05-14T08:37:38.651487300Z Tick: 2019-05-14T08:35:00Z
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofMinutes(1));
tickMinutes
method instead.
Clock clock = Clock.tickMinutes(zoneId);
Duration in Hours
When the duration is passed 2 hours totick
method, the clock will give current instant truncated to the nearest occurrence of 2 hours duration and the minimum time unit will be hour. We will get truncated instant lesser than base instant. The next tick of clock will take place only after 2 hours and until then instant will not change.
DurationInHours.java
package com.concretepage; import java.time.Clock; import java.time.Duration; public class DurationInHours { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); Clock tickClock = Clock.tick(baseClock, Duration.ofHours((2))); for (int i = 1; i <= 3; i++) { System.out.println("-----" + i + "-----"); System.out.println("Base: " + baseClock.instant()); System.out.println("Tick: " + tickClock.instant()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
-----1----- Base: 2019-05-14T08:47:58.169921700Z Tick: 2019-05-14T08:00:00Z -----2----- Base: 2019-05-14T08:48:00.192037400Z Tick: 2019-05-14T08:00:00Z -----3----- Base: 2019-05-14T08:48:02.192151800Z Tick: 2019-05-14T08:00:00Z
References
Java Doc: Class ClockJava Clock