abstract class Clock extends AnyRef
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()
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
ZoneId) fixed
or Duration) 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.
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.
- Alphabetic
- By Inheritance
- Clock
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Clock()
- Attributes
- protected
Abstract Value Members
- abstract def getZone: ZoneId
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.
- returns
the time-zone being used to interpret instants, not null
- abstract def instant: Instant
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.
- returns
the current instant from this clock, not null
- Exceptions thrown
DateTimeException
if the instant cannot be obtained, not thrown by most implementations
- abstract def withZone(zone: ZoneId): Clock
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.
- zone
the time-zone to change to, not null
- returns
a clock based on this clock with the specified time-zone, not null
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(obj: Any): Boolean
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.
- obj
the object to check, null returns false
- returns
true if this is equal to the other clock
- Definition Classes
- Clock → AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
A hash code for this clock.
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def millis: Long
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()
.- returns
the current millisecond instant from this clock, measured from the Java epoch of 1970-01-01T00:00 UTC, not null
- Exceptions thrown
DateTimeException
if the instant cannot be obtained, not thrown by most implementations
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()