Gets the time-zone being used to create dates and times.
Gets the time-zone being used to create dates and times.
A clock will typically obtain the current instant and then convert that to a date or time using a time-zone. This method returns the time-zone used.
the time-zone being used to interpret instants, not null
Gets the current instant of the clock.
Gets the current instant of the clock.
This returns an instant representing the current instant as defined by the clock.
the current instant from this clock, not null
DateTimeException
if the instant cannot be obtained, not thrown by most implementations
Returns a copy of this clock with a different time-zone.
Returns a copy of this clock with a different time-zone.
A clock will typically obtain the current instant and then convert that to a date or time using a time-zone. This method returns a clock with similar properties but using a different time-zone.
the time-zone to change to, not null
a clock based on this clock with the specified time-zone, not null
Checks if this clock is equal to another clock.
Checks if this clock is equal to another clock.
Clocks must compare equal based on their state and behavior.
the object to check, null returns false
true if this is equal to the other clock
A hash code for this clock.
Gets the current millisecond instant of the clock.
Gets the current millisecond instant of the clock.
This returns the millisecond-based instant, measured from 1970-01-01T00:00 UTC.
This is equivalent to the definition of System#currentTimeMillis()
.
Most applications should avoid this method and use Instant
to represent
an instant on the time-line rather than a raw millisecond value.
This method is provided to allow the use of the clock in high performance use cases
where the creation of an object would be unacceptable.
The default implementation currently calls #instant()
.
the current millisecond instant from this clock, measured from the Java epoch of 1970-01-01T00:00 UTC, not null
DateTimeException
if the instant cannot be obtained, not thrown by most implementations
A clock providing access to the current instant, date and time using a time-zone.
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()
andTimeZone#getDefault()
.Use of a
Clock
is optional. All key date-time classes also have anow()
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
This approach allows an alternate clock, such asClock
into any method that requires the current instant. A dependency injection framework is one way to achieve this:ZoneId) fixed
orDuration) offset
to be used during testing.The
system
factory methods provide clocks based on the best available system clock This may useSystem#currentTimeMillis()
, or a higher resolution clock if one is available.Specification for implementors
This abstract class must be implemented with care to ensure other operate correctly. All implementations that can be instantiated must be final, immutable and thread-safe.
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. If the implementation wraps a source that provides leap second information, then a mechanism should be used to "smooth" the leap second, such as UTC-SLS.Implementations should implement
Serializable
wherever possible and must document whether or not they do support serialization.