Class/Object

java.time

Clock

Related Docs: object Clock | package time

Permalink

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.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Clock
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Clock()

    Permalink
    Attributes
    protected

Abstract Value Members

  1. abstract def getZone: ZoneId

    Permalink

    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

  2. abstract def instant: Instant

    Permalink

    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

  3. abstract def withZone(zone: ZoneId): Clock

    Permalink

    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

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(obj: Any): Boolean

    Permalink

    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
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

    Permalink

    A hash code for this clock.

    A hash code for this clock.

    returns

    a suitable hash code

    Definition Classes
    Clock → AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. def millis: Long

    Permalink

    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

  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  17. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  18. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped