APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.time

public abstract Class Clock

extends Object
implements InstantSource
Class Inheritance
All Implemented Interfaces
java.time.InstantSource
Known Direct Subclasses
java.time.Clock.SystemClock, java.time.Clock.FixedClock, java.time.Clock.OffsetClock, java.time.Clock.TickClock, java.time.Clock.SourceClock
Imports
java.io.ObjectStreamException, .Serializable, java.util.Objects, .TimeZone, jdk.internal.misc.VM

A clock providing access to the current instant, date and time using a time-zone.

Instances of this abstract class are used to access a pluggable representation of the current instant, which can be interpreted using the stored time-zone to find the current date and time. For example, Clock can be used instead of System#currentTimeMillis() and TimeZone#getDefault().

Use of a Clock is optional. All key date-time classes also have a now() factory method that uses the system clock in the default time zone. The primary purpose of this abstraction is to allow alternate clocks to be plugged in as and when required. Applications use an object to obtain the current time rather than a static method. This can simplify testing.

As such, this abstract class does not guarantee the result actually represents the current instant on the time-line. Instead, it allows the application to provide a controlled view as to what the current instant and time-zone are.

Best practice for applications is to pass a Clock into any method that requires the current instant and time-zone. A dependency injection framework is one way to achieve this:

 public class MyBean {
   private Clock clock;  // dependency inject
   ...
   public void process(LocalDate eventDate) {
     if (eventDate.isBefore(LocalDate.now(clock)) {
       ...
     }
   }
 }
This approach allows an alternative clock, such as fixed or offset to be used during testing.

The system factory methods provide clocks based on the best available system clock. This may use System#currentTimeMillis(), or a higher resolution clock if one is available.

Implementation Specification

This abstract class must be implemented with care to ensure other classes operate correctly. All implementations must be thread-safe - a single instance must be capable of be invoked from multiple threads without negative consequences such as race conditions.

The principal methods are defined to allow the throwing of an exception. In normal use, no exceptions will be thrown, however one possible implementation would be to obtain the time from a central time server across the network. Obviously, in this case the lookup could fail, and so the method is permitted to throw an exception.

The returned instants from Clock work on a time-scale that ignores leap seconds, as described in Instant. If the implementation wraps a source that provides leap second information, then a mechanism should be used to "smooth" the leap second. The Java Time-Scale mandates the use of UTC-SLS, however clock implementations may choose how accurate they are with the time-scale so long as they document how they work. Implementations are therefore not required to actually perform the UTC-SLS slew or to otherwise be aware of leap seconds.

Implementations should implement Serializable wherever possible and must document whether or not they do support serialization.

Since
1.8
See Also
InstantSource

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Clock.FixedClock

Implementation of a clock that always returns the same instant.

pack-priv static class
Clock.OffsetClock

Implementation of a clock that adds an offset to an underlying clock.

pack-priv static class
Clock.SourceClock

Implementation of a clock based on an InstantSource.

pack-priv static class
Clock.SystemClock

Implementation of a clock that always returns the latest time from SystemInstantSource.INSTANCE.

pack-priv static class
Clock.SystemInstantSource

An instant source that always returns the latest time from System#currentTimeMillis() or equivalent.

pack-priv static class
Clock.TickClock

Implementation of a clock that reduces the tick frequency of an underlying clock.

Field Summary

Modifier and TypeField and Description
private static long
private static final long

Constructor Summary

AccessConstructor and Description
protected
Clock()

Constructor accessible by subclasses.

Method Summary

Modifier and TypeMethod and Description
pack-priv static Instant
public boolean

Returns:

true if this is equal to the other clock
equals
(Object
the object to check, null returns false
obj
)

Overrides java.lang.Object.equals.

Checks if this clock is equal to another clock.

public static Clock

Returns:

a clock that always returns the same instant, not null
fixed
(Instant
the instant to use as the clock, not null
fixedInstant
,
ZoneId
the time-zone to use to convert the instant to date-time, not null
zone
)

Obtains a clock that always returns the same instant.

public abstract ZoneId

Returns:

the time-zone being used to interpret instants, not null
getZone
()

Gets the time-zone being used to create dates and times.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this clock.

public abstract Instant

Returns:

the current instant from this clock, not null
instant
()

Redeclares java.time.InstantSource.instant.

Gets the current instant of the clock.

public long

Returns:

the current millisecond instant from this clock, measured from the Java epoch of 1970-01-01T00:00Z (UTC), not null
millis
()

Overrides default java.time.InstantSource.millis.

Gets the current millisecond instant of the clock.

public static Clock

Returns:

a clock based on the base clock with the duration added, not null
offset
(Clock
the base clock to add the duration to, not null
baseClock
,
Duration
the duration to add, not null
offsetDuration
)

Obtains a clock that returns instants from the specified clock with the specified duration added.

public static Clock

Returns:

a clock that uses the best available system clock in the specified zone, not null
system
(ZoneId
the time-zone to use to convert the instant to date-time, not null
zone
)

Obtains a clock that returns the current instant using the best available system clock.

public static Clock

Returns:

a clock that uses the best available system clock in the default zone, not null
systemDefaultZone
()

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

public static Clock

Returns:

a clock that uses the best available system clock in the UTC zone, not null
systemUTC
()

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the UTC time-zone.

public static Clock

Returns:

a clock that ticks in whole units of the duration, not null
tick
(Clock
the base clock to base the ticking clock on, not null
baseClock
,
Duration
the duration of each visible tick, not negative, not null
tickDuration
)

Obtains a clock that returns instants from the specified clock truncated to the nearest occurrence of the specified duration.

public static Clock

Returns:

a clock that ticks in whole milliseconds using the specified zone, not null
tickMillis
(ZoneId
the time-zone to use to convert the instant to date-time, not null
zone
)

Obtains a clock that returns the current instant ticking in whole milliseconds using the best available system clock.

public static Clock

Returns:

a clock that ticks in whole minutes using the specified zone, not null
tickMinutes
(ZoneId
the time-zone to use to convert the instant to date-time, not null
zone
)

Obtains a clock that returns the current instant ticking in whole minutes using the best available system clock.

public static Clock

Returns:

a clock that ticks in whole seconds using the specified zone, not null
tickSeconds
(ZoneId
the time-zone to use to convert the instant to date-time, not null
zone
)

Obtains a clock that returns the current instant ticking in whole seconds using the best available system clock.

public abstract Clock

Returns:

a clock based on this clock with the specified time-zone, not null
withZone
(ZoneId
the time-zone to change to, not null
zone
)

Redeclares as abstract: java.time.InstantSource.withZone.

Returns a copy of this clock with a different time-zone.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait