Clock tick() Method in Java with Examples
Last Updated :
20 Apr, 2023
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 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.
Syntax:
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.
Example:
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.
Java
// Java program to demonstrate
// tick() method of Clock class
import java.time.*;
// create class
public class tickMethodDemo {
// Main method
public static void main(String[] args)
{
// create base Clock with systemDefaultZone() method
Clock baseclock = Clock.systemDefaultZone();
// get instant of the base class and print instant
Instant instant = baseclock.instant();
System.out.println("Instant of Base class " + instant);
// apply tick Method for Duration of 30sec and
// create clock1 and print instant of clock1
Clock clock1 = Clock.tick(baseclock, Duration.ofSeconds(30));
System.out.println("Instant of Clock1 " + clock1.instant());
// apply tick Method for Duration of 10sec and
// create clock2 and print instant of clock2
Clock clock2 = Clock.tick(baseclock, Duration.ofSeconds(10));
System.out.println("Instant of Clock2 " + clock2.instant());
// apply tick Method for Duration of 3sec and
// create clock3 and print instant of clock3
Clock clock3 = Clock.tick(baseclock, Duration.ofSeconds(3));
System.out.println("Instant of Clock3 " + clock3.instant());
}
}
Output: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.
Java
// Java program to demonstrate
// tick() method of Clock class
import java.time.*;
// create class
public class tickMethodDemo {
// Main method
public static void main(String[] args)
{
// create base Clock with systemUTC() method
Clock baseclock = Clock.systemUTC();
// get instant of the base class and print instant
Instant instant = baseclock.instant();
System.out.println("Instant of Base class "
+ instant);
// apply tick Method for Duration of 10 Minutes
Clock clock1 = Clock.tick(baseclock,
Duration.ofMinutes(10));
System.out.println("Instant of Clock1 when duration"
+ " = 10 minutes is "
+ clock1.instant());
// apply tick Method for Duration of 2 hours
Clock clock2 = Clock.tick(baseclock,
Duration.ofHours(2));
System.out.println("Instant of Clock2 when duration"
+ " = 2 hours is "
+ clock2.instant());
// apply tick Method for Duration of 5 days
Clock clock3 = Clock.tick(baseclock,
Duration.ofDays(5));
System.out.println("Instant of Clock2 when duration"
+ " = 5 days is "
+ clock3.instant());
}
}
Output: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
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#tick-java.time.Clock-java.time.Duration-
Similar Reads
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 immutabl
2 min read
Clock system() Method in Java with Examples
java.time.Clock.system(ZoneId zone) method is a static method of Clock class which returns a clock that returns the current instant of the clock using best available system clock with ZoneID of the returned clock is set to the ZoneID passed. This method can use System.currentTimeMillis(), or other h
3 min read
Clock systemUTC() Method in Java with Examples
java.time.Clock.systemUTC() method is a static method of Clock class which returns a clock that returns the current instant of the clock using the best available system clock where Zone of the returned clock is UTC time-zone. When the current instant is needed without the date or time, then use syst
2 min read
Java Clock tickMinutes() method in Java with Examples
java.time.Clock.tickMinutes(ZoneId zone) method is a static method of Clock class that returns the current instant ticking in whole minutes using the best available system clock and the time-zone of that instant is same as the time-zone passed as a parameter. Nanosecond and second fields of the cloc
3 min read
Java 8 Clock instant() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. instant() method of Clock class returns a current instant of Clock object as Instant Class Object. Instant generates a timestamp to represent machine time. So this method genera
3 min read
Java 8 Clock fixed() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. fixed() method of Clock class returns a clock object and the Clock object returns the same instant. Clock object is returned by calling Clock.fixed(parameters) simply returns th
2 min read
Java 8 Clock offset() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. The offset() method is a static method of Clock class which returns a clock with instant equal to the sum of the instants of clock passed as parameter and specific Offset durati
3 min read
Java 8 Clock millis() Method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.The millis() method of Clock class returns the current instant of the clock in milliseconds. A millisecond instant is measured from 1970-01-01T00:00Z (UTC) to the current time. T
2 min read
MonthDay now(Clock) method in Java with Examples
The now(Clock clock) method of the MonthDay class in Java is used to get the current month-day from the specified clock. Syntax: public static MonthDay now(Clock clock) Parameters: This method accepts clock as parameter which represents the clock to use. Return value: This method returns the current
1 min read
Java 8 Clock getZone() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.getZone() method of Clock class returns the time-zone used to create dates and times of Clock class. Every Clock class needs a Time Zone for obtaining the current instant of time
2 min read