tick(Clock baseClock, Duration tickDuration) method of java.time.Clock is a static method of Clock class that 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 the duration.
This method can help to customize the clock class which tick according to duration. E.g. a tick means if provided the duration 5 seconds, then the second part of instant will be rounded according to 5 sec. If the 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 the 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)); Explanation:: 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 the below program, there are three cases where duration is 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 the clock when the 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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.