Clock

Clock is an abstract class that provides access to the current instant (which we will see next), date, and time using a timezone. Prior to Java 8, we would have to call System.currentTimeInMillis() and TimeZone.getDefault() to calculate these values. Clock provides a nice interface to get that from one object.

The Javadoc states that the use of Clock is purely optional. In fact, the major date/time classes have a now() method that uses the system clock to get their value. If, however, you need to provide an alternate implementation (say, in testing, you need the LocalTime in another timezone), this abstract class can be extended to provide the functionality needed, and can then be passed to the appropriate now() method.

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.