public abstract class Clock extends Object
Instances of this class are used to find the current instant, which can be
interpreted using the stored time-zone to find the current date and time.
As such, a 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.
Best practice for applications is to pass a Clock
into any method
that requires the current instant. 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 alternate 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.
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.
System.currentTimeMillis()
.
That method provides little to no guarantee about the accuracy of the clock.
Applications requiring a more accurate clock must implement this abstract class
themselves using a different external clock, such as an NTP server.Modifier | Constructor | Description |
---|---|---|
protected |
Clock() |
Constructor accessible by subclasses.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
equals(Object obj) |
Checks if this clock is equal to another clock.
|
static Clock |
fixed(Instant fixedInstant,
ZoneId zone) |
Obtains a clock that always returns the same instant.
|
abstract ZoneId |
getZone() |
Gets the time-zone being used to create dates and times.
|
int |
hashCode() |
A hash code for this clock.
|
abstract Instant |
instant() |
Gets the current instant of the clock.
|
long |
millis() |
Gets the current millisecond instant of the clock.
|
static Clock |
offset(Clock baseClock,
Duration offsetDuration) |
Obtains a clock that returns instants from the specified clock with the
specified duration added
|
static Clock |
system(ZoneId zone) |
Obtains a clock that returns the current instant using best available
system clock.
|
static Clock |
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.
|
static Clock |
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.
|
static Clock |
tick(Clock baseClock,
Duration tickDuration) |
Obtains a clock that returns instants from the specified clock truncated
to the nearest occurrence of the specified duration.
|
static Clock |
tickMinutes(ZoneId zone) |
Obtains a clock that returns the current instant ticking in whole minutes
using best available system clock.
|
static Clock |
tickSeconds(ZoneId zone) |
Obtains a clock that returns the current instant ticking in whole seconds
using best available system clock.
|
abstract Clock |
|