Java Clock Class – java.time.Clock

Filed Under: Java

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

  1. Java Clock class is in java.time package.
  2. Java Clock is an abstract class, so we can’t instantiate it. However it contains several static methods to get it’s instance.
  3. 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.
    
    public class MySpringBean {
        @Autowired
        private Clock clock; 
    }
    

    Best practice is to pass a Clock as argument into method that requires the current instant.

  4. Clock can be used instead of System.currentTimeMillis() and TimeZone.getDefault().
  5. Java provides four implementations of Clock – FixedClock, OffsetClock, SystemClock and TickClock. They are part of Clock class and there are static methods that return these Clock implementations.
    Java Clock Class 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.


Clock clock = Clock.systemDefaultZone();
System.out.println(clock.getZone()); // prints "Asia/Kolkata" for me

instant()

This method returns the current instant of the clock.


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.


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.


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().


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.


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.


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:


2018-05-08T17:51:09.116566Z
2018-05-08T17:00:00Z
TickClock[SystemClock[Asia/Kolkata],PT1S]
2018-05-08T17:51:09Z

Notice the usage of TickClock and SystemClock instances.

fixed(Instant fixedInstant, ZoneId zone)

This method returns a Clock that always returns the same instant.


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:


FixedClock[2018-05-08T17:51:09.102302Z,Asia/Kolkata]
2018-05-08T17:51:09.102302Z
2018-05-08T17:51:09.102302Z

That’s all for Java Clock class usage with examples.

You can download the complete code from my GitHub Repository.

Reference: API Doc

Comments

  1. Rameez Raja says:

    Hi Pankaj,.

    Glad to see this tutorial and thank your for sharing this.

    I have simulated the same scenario but I can able to see only 3 digits in second field but in your example I can see 6 digits.

    My output : 2018-12-03T10:18:43.448Z
    your output : “2018-05-08T17:51:09.116133Z”

    Basically I want to achieve upto micro seconds using Java 8 clock api.

    Kindly help

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages