Java Clock class is part of Date Time API. Java Clock class is used to get the current instance, date and time with the time zone.
Java Clock Class
- Java Clock class is in
java.time
package. - Java Clock is an abstract class, so we can’t instantiate it. However it contains several static methods to get it’s instance.
- Java Clock class usage is optional, because most of the Date API classes has
now()
method. It’s main purpose is to allow alternate clocks to be plugged in as and when required. For example in Spring Dependency Injection.
123456public class MySpringBean {@Autowiredprivate Clock clock;}
Best practice is to pass a Clock as argument into method that requires the current instant. - Clock can be used instead of
System.currentTimeMillis()
andTimeZone.getDefault()
. - Java provides four implementations of Clock –
FixedClock
,OffsetClock
,SystemClock
andTickClock
. They are part of Clock class and there are static methods that return these Clock implementations.
Java Clock Methods
Let’s look into Clock class static methods and their usage.
systemDefaultZone()
Clock systemDefaultZone() method returns the Clock instance with system default time zone.
1 2 3 4 |
Clock clock = Clock.systemDefaultZone(); System.out.println(clock.getZone()); // prints "Asia/Kolkata" for me |
instant()
This method returns the current instant of the clock.
1 2 3 4 |
Instant instant = clock.instant(); System.out.println(instant); //prints "2018-05-08T17:51:09.102302Z" |
systemUTC()
This method returns the Clock instance with UTC time zone.
1 2 3 4 |
clock = Clock.systemUTC(); System.out.println(clock.getZone()); //prints "Z" |
system(ZoneId zone)
This method is used to get the Clock instance with specified time zone.
1 2 3 4 |
Clock clock = Clock.system(ZoneId.of("Europe/Paris")); System.out.println(clock.instant()); // prints "2018-05-08T17:51:09.116133Z" |
millis()
This method returns the current milliseconds of the instance. It’s equivalent to System.currentTimeMillis()
.
1 2 3 4 |
System.out.println(clock.millis()); //prints 1525801869116 System.out.println(System.currentTimeMillis()); // prints 1525801869116 |
offset(Clock baseClock, Duration offsetDuration)
This method is used to get a Clock with instance added to the given base clock. We can used it to simulate future and past time testing.
1 2 3 4 5 6 |
Clock pastClock = Clock.offset(clock, Duration.ofMillis(-10000)); System.out.println(clock.millis() - pastClock.millis()); //prints 10000 Clock futureClock = Clock.offset(clock, Duration.ofDays(1)); System.out.println(futureClock.millis() - clock.millis()); //prints 86400000 |
tick(Clock baseClock, Duration tickDuration)
This method returns Clock that returns instants from the specified base clock truncated to the nearest occurrence of the specified duration. Let’s see it’s usage with an example code snippet.
1 2 3 4 5 6 7 8 |
Clock nearestHourClock = Clock.tick(clock, Duration.ofHours(1)); System.out.println(clock.instant()); System.out.println(nearestHourClock.instant()); Clock nearestSecondClock = Clock.tickSeconds(ZoneId.systemDefault()); System.out.println(nearestSecondClock); System.out.println(nearestSecondClock.instant()); |
Output of above code snippet:
1 2 3 4 5 6 |
<span style="color: #008000;"><strong><code> 2018-05-08T17:51:09.116566Z 2018-05-08T17:00:00Z TickClock[SystemClock[Asia/Kolkata],PT1S] 2018-05-08T17:51:09Z </code></strong></span> |
Notice the usage of TickClock
and SystemClock
instances.
fixed(Instant fixedInstant, ZoneId zone)
This method returns a Clock that always returns the same instant.
1 2 3 4 5 6 7 |
Clock fixedClock = Clock.fixed(instant, ZoneId.systemDefault()); System.out.println(fixedClock); System.out.println(fixedClock.instant()); Thread.sleep(1000); System.out.println(fixedClock.instant()); |
Output:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> FixedClock[2018-05-08T17:51:09.102302Z,Asia/Kolkata] 2018-05-08T17:51:09.102302Z 2018-05-08T17:51:09.102302Z </code></strong></span> |
That’s all for Java Clock class usage with examples.
Reference: API Doc