tick(Clock baseClock, Duration tickDuration) method of java.time.Clock is a static method of Clock class which returns a clock that returns instants from the base clock rounded to the nearest occurrence of the specified duration in the parameter. The specified base clock duration must be positive, neither negative, nor null. The clock will tick as per as the given duration and if the duration is half of the minute or half of the second or half of the hour, the clock will return instants rounded to the half of minute or half of the second or half of the hour, as specified by duration.
This method can help to customize clock class which tick according to duration. e.g tick means if provided the duration 5 seconds then the second part of instant will be rounded according to 5 sec. If base clock second part is 13, then it will be 10. Rounded clock returns instant which is smaller than the base clock.
A duration of zero or one nanosecond would have no effect on base clock method. These will return the same base clock. If base clock is immutable, thread-safe and Serializable, then the returned clock will also be immutable, thread-safe and Serializable.
public static Clock tick(Clock baseClock, Duration tickDuration)
Parameters: This method takes two mandatory parameters:
- baseClock – the base clock which you want to roundoff according to duration.
- tickDuration – the duration of each visible tick to roundoff the clock, not negative, not null.
Return Value: This method returns a clock that uses the best available system clock in the default zone
Exception: This method throws following exceptions:
- IllegalArgumentException – if the duration is negative, or has a part which is very smaller
than a whole millisecond such that the whole duration is not divisible into one second.
- ArithmeticException – if the duration is too large to be represented in nanoseconds.
Code: Clock baseClock = Clock.systemDefaultZone(); Clock clock = Clock.tick(baseClock, Duration.ofSeconds(10)); Expalnation:: method tick() returns the instant which ticks after per 10 sec means the duration of the tick is 10sec.is instant actual time is 18:57:51.248Z then due to 10sec duration it will round to 18:57:50Z and same for 18:59:36.247Z to 18:59:30Z.
Below programs illustrates tick() method of java.time.Clock class
Program 1: When Clock is created with systemDefaultZone().
In below program there are three cases where duration are 30sec, 10 sec, 3sec. So tick() method has to be applied for these three cases.
Instant of Base class 2018-08-22T11:18:11.336Z Instant of Clock1 2018-08-22T11:18:00Z Instant of Clock2 2018-08-22T11:18:10Z Instant of Clock3 2018-08-22T11:18:09Z
Program 2: Print the instant of clock when duration is in Minutes, Hours Or Days.
Instant of Base class 2018-08-22T11:18:15.533Z Instant of Clock1 when duration = 10 minutes is 2018-08-22T11:10:00Z Instant of Clock2 when duration = 2 hours is 2018-08-22T10:00:00Z Instant of Clock2 when duration = 5 days is 2018-08-22T00:00:00Z
- Clock systemDefaultZone() Method in Java with Examples
- Clock tickSeconds() method in Java with Examples
- Clock systemUTC() Method in Java with Examples
- Clock system() Method in Java with Examples
- Java 8 Clock equals() Method with Examples
- Java 8 Clock millis() Method with Examples
- Java 8 Clock fixed() method with Examples
- Java 8 Clock offset() method with Examples
- Java 8 Clock getZone() method with Examples
- Java 8 Clock hashCode() method with Examples
- Java 8 Clock instant() method with Examples
- Java Clock withZone() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Android | How to display Analog clock and Digital clock
- Java Applet | How to display a Digital Clock
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.