Introduction To Java Clock Class

Posted By : Rahul Chauhan | 17-Jun-2019

1. Overview
Here, we’re going to look into the Java Clock class from the java.time package. We will explain what the Clock class is and how we can use it.

 

2. The Clock Class

Clock was additional in Java eight and provides access to a moment in time victimization the simplest on the market system clock, and to be used as a time provider which can be effectively stubbed for testing purposes.

The current date and time based on the time zone and, for globalized applications, a time provider is necessary to ensure that the date and time are created with the correct time zone.

 

This class helps us to test our code work with different time zones or – when using a fixed clock – that time doesn’t affect our code.

The Clock class is a abstract class, so we cannot create an instance of it. The factory methods can be used are following:

offset(Clock, Duration) – returns a clock that is offset by the specified Duration. The main use case for this is to simulate running in future or the past
systemUTC() – returns a clock representing the UTC time zone
fixed(Instant, ZoneId) – always returns the same Instant. The leading use case for this is in testing, where the fixed clock ensures that tests are not dependent on the current clock.


We’re going to look into most of the methods available in the Clock class.

2.1. instant()
This technique returns a moment representing this instant outlined by the clock:

 Clock clock = Clock.systemDefaultZone();  
 Instant instant = clock.instant();  
 System.out.println(instant);  

will produce:
2018-04-07T03:59:35.555Z

 

2.2. systemUTC()
This technique returns a Clock object representing this instant within the UT zone:

 Clock clock = Clock.systemUTC();  
 System.out.println("UTC time ::" + clock.instant());  

will produce:
UTC time :: 2018-04-04T17:40:12.353Z

 

2.3. system()
This static technique returns the Clock object for the timezone known by the given geographical zone id:

 Clock clock = Clock.system(ZoneId.of("Asia/Calcutta"));  
 System.out.println(clock.instant());  

will produce:
2018-04-04T18:00:31.376Z

 

2.4. systemDefaultZone()
This static technique returns a Clock object representing this instant and victimization the default geographical zone of the system it’s running on:

 Clock clock = Clock.systemDefaultZone();  
 System.out.println(clock);  

The above lines produce the following result (assuming our default time zone is “Asia/Calcutta”):
SystemClock[Asia/Calcutta]
We can achieve the same behavior by passing ZoneId.systemDefault():

 Clock clock = Clock.system(ZoneId.systemDefault());  

2.5. millis()
This technique returns this instant of the punch in milliseconds. It’s provided to allow the use of the clock in high-performance use cases where the creation of an object would be unacceptable. This technique may be utilized in places wherever we’d otherwise have used

 System.currentTimeInMillis():  
 Clock clock = Clock.systemDefaultZone();  
 System.out.println(clock.millis());  

will produce:
1523104441258

 

2.6. offset()

This static method returns an instant from the specified base clock with the specified duration added.
If the period is negative, then the resulting clock instant will be earlier than the given base clock.

Using offset, we can get instants in the past and future of the given base clock. If we tend to pass a zero period then we’ll get constant clock as given base clock:

 Clock baseClock = Clock.systemDefaultZone();  
 // result clock will be later than baseClock  
 Clock clock = Clock.offset(baseClock, Duration.ofHours(72));  
 System.out.println(clock.instant());  
 // result clock will be same as baseClock  
 clock = Clock.offset(baseClock, Duration.ZERO);  
 System.out.println(clock.instant());  
 // result clock will be earlier than baseClock  
 clock = Clock.offset(baseClock, Duration.ofHours(-72));  
 System.out.println(clock.instant());  

will produce:
2018-04-10T13:24:07.347Z
2018-04-07T13:24:07.348Z
2018-04-04T13:24:07.348Z

 

2.7. tick()
This static technique returns instants from the desired clock rounded to the closest prevalence of the desired period. The specified clock duration must be positive:

 Clock clockDefaultZone = Clock.systemDefaultZone();  
 Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(30));  
 System.out.println("Clock Default Zone: " + clockDefaultZone.instant());  
 System.out.println("Clock tick: " + clocktick.instant());  

will produce:
Clock Default Zone: 2018-04-07T16:42:05.473Z
Clock tick: 2018-04-07T16:42:00Z

 

2.8. tickSeconds()
This static technique returns this instant ticking in whole seconds for the given geographical zone. This clock can continuously have the nano-of-second field set to zero:

 ZoneId zoneId = ZoneId.of("Asia/Calcutta");  
 Clock clock = Clock.tickSeconds(zoneId);  
 System.out.println(clock.instant());  

will produce:
2018-04-07T17:40:23Z
The same can be achieved by using tick():

 Clock clock = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofSeconds(1));  

2.9. tickMinutes()
This static technique returns the clock instant ticking in whole minutes for the desired timezone. This clock will always have the nano-of-second and second-of-minute fields set to zero:

 ZoneId zoneId = ZoneId.of("Asia/Calcutta");  
 Clock clock = Clock.tickMinutes(zoneId);  
 System.out.println(clock.instant());  

will produce:
2018-04-07T17:26:00Z
The same can be achieved by using tick():

 Clock clock = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofMinutes(1));  

2.10. withZone()
This technique returns a replica of this clock with a unique geographical zone.
If we've a clock instance for a selected geographical zone, we can make a copy of that clock for different time zone:

 ZoneId zoneSingapore = ZoneId.of("Asia/Singapore");  
 Clock clockSingapore = Clock.system(zoneSingapore);  
 System.out.println(clockSingapore.instant());  
 ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta");  
 Clock clockCalcutta = clockSingapore.withZone(zoneCalcutta);  
 System.out.println(clockCalcutta.instant());  

will produce:
2018-04-07T17:55:43.035Z
2018-04-07T17:55:43.035Z

 

2.11. getZone()
This technique returns the geographical zone of the given Clock.

 Clock clock = Clock.systemDefaultZone();  
 ZoneId zone = clock.getZone();  
 System.out.println(zone.getId());  

will produce:
Asia/Calcutta

 

2.12. fixed()
This technique returns a clock that continuously returns constant instant. The main use case for this method is in testing, where the fixed clock ensures that tests are not dependent on the current clock.

 Clock fixedClock = Clock.fixed(Instant.parse("2018-04-29T10:15:30.00Z"),  
 ZoneId.of("Asia/Calcutta"));  
 System.out.println(fixedClock);  

will produce:
FixedClock[2018-04-29T10:15:30Z,Asia/Calcutta]

 

3. Conclusion
In this article, we tend to dove into the Java Clock category and therefore the alternative ways we will use it with on the market strategies.

 

Thanks

About Author

Author Image
Rahul Chauhan

Rahul Chauhan is Java Developer having good knowledge of java, have a knowledge of Spring Boot.

Request for Proposal

Name is required

Comment is required

Sending message..