src/share/classes/java/time/Clock.java
Print this page
@@ -375,64 +375,61 @@
* <p>
* Most applications should avoid this method and use {@link 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.
+ * <p>
+ * The default implementation currently calls {@link #instant}.
*
* @return the current millisecond instant from this clock, measured from
* the Java epoch of 1970-01-01T00:00 UTC, not null
* @throws DateTimeException if the instant cannot be obtained, not thrown by most implementations
*/
- public abstract long millis();
+ public long millis() {
+ return instant().toEpochMilli();
+ }
//-----------------------------------------------------------------------
/**
* Gets the current instant of the clock.
* <p>
* This returns an instant representing the current instant as defined by the clock.
- * <p>
- * The default implementation currently calls {@link #millis}.
*
* @return the current instant from this clock, not null
* @throws DateTimeException if the instant cannot be obtained, not thrown by most implementations
*/
- public Instant instant() {
- return Instant.ofEpochMilli(millis());
- }
+ public abstract Instant instant();
//-----------------------------------------------------------------------
/**
* Checks if this clock is equal to another clock.
* <p>
- * Clocks must compare equal based on their state and behavior.
+ * Clocks should override this method to compare equals based on
+ * their state and to meet the contract of {@link Object#equals}.
+ * If not overridden, the behavior is defined by {@link Object#equals}
*
* @param obj the object to check, null returns false
* @return true if this is equal to the other clock
*/
@Override
- public abstract boolean equals(Object obj);
+ public boolean equals(Object obj) {
+ return super.equals(obj);
+ }
/**
* A hash code for this clock.
- *
- * @return a suitable hash code
- */
- @Override
- public abstract int hashCode();
-
- //-----------------------------------------------------------------------
- /**
- * Returns a string describing this clock.
* <p>
- * Clocks must have a string representation based on their state and behavior.
- * For example, 'System[Europe/Paris]' could be used to represent the System
- * clock in the 'Europe/Paris' time-zone.
+ * Clocks should override this method based on
+ * their state and to meet the contract of {@link Object#hashCode}.
+ * If not overridden, the behavior is defined by {@link Object#hashCode}
*
- * @return a string representation of this clock, not null
+ * @return a suitable hash code
*/
@Override
- public abstract String toString();
+ public int hashCode() {
+ return super.hashCode();
+ }
//-----------------------------------------------------------------------
/**
* Implementation of a clock that always returns the latest time from
* {@link System#currentTimeMillis()}.
@@ -458,10 +455,14 @@
@Override
public long millis() {
return System.currentTimeMillis();
}
@Override
+ public Instant instant() {
+ return Instant.ofEpochMilli(millis());
+ }
+ @Override
public boolean equals(Object obj) {
if (obj instanceof SystemClock) {
return zone.equals(((SystemClock) obj).zone);
}
return false;