Class Timestamp
- All Implemented Interfaces:
Comparable<Timestamp>
Values are stored as an unsigned 32 bit integer, which therefore recycles after 232 milliseconds (about 48 days). Two values to be compared must have been generated within 24 days of each other to be ordered correctly.
This class uses System.nanoTime()
, not System.currentTimeMillis()
, and so is immune to changes in the system clock.
To facilitate debugging, the zero mark is set at class initialization time.
Instances are immutable.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Comparator<Timestamp>
Sorts possibly nullTimestamp
s in chronological order, with null sorting first. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Compare two instances, where "smaller" means earlier in time.boolean
int
Get the relative milliseconds value contained by this instance.int
hashCode()
boolean
Determine whether this timestamp is in the past or the future.boolean
Determine whether this timestamp is so far in the past that it is in danger of rolling over to the future as time continues to move forward.offset
(int offset) Return this timestamp offset by the given amount.int
offsetFrom
(Timestamp base) Get the number of milliseconds this instance is offset from the given instance.int
Get the number of milliseconds this instance is offset from the current time.toString()
-
Field Details
-
NULL_FIRST_SORT
-
-
Constructor Details
-
Timestamp
public Timestamp()Constructor returning the current time. -
Timestamp
public Timestamp(int millis) Constructor.- Parameters:
millis
- relative milliseconds value fromgetMillis()
-
-
Method Details
-
getMillis
public int getMillis()Get the relative milliseconds value contained by this instance.- Returns:
- relative millisecond value
-
offsetFromNow
public int offsetFromNow()Get the number of milliseconds this instance is offset from the current time.A positive offset means this instance is in the future.
- Returns:
- relative millisecond offset
-
offsetFrom
Get the number of milliseconds this instance is offset from the given instance.A positive offset means this instance is after
base
.- Parameters:
base
- base timestamp- Returns:
- relative millisecond offset
- Throws:
IllegalArgumentException
- ifbase
is null
-
offset
Return this timestamp offset by the given amount.- Parameters:
offset
- offset in milliseconds- Returns:
- adjusted timestamp
-
hasOccurred
public boolean hasOccurred()Determine whether this timestamp is in the past or the future.- Returns:
- true if this timestamp is in the past
-
isRolloverDanger
public boolean isRolloverDanger()Determine whether this timestamp is so far in the past that it is in danger of rolling over to the future as time continues to move forward.This returns true if this timestamp is within 5% of the roll-over point relative to the current time. This would represent a time approximately 22.8 days in the past.
- Returns:
- true if this timestamp's offset is dangerously negative
-
equals
-
hashCode
public int hashCode() -
toString
-
compareTo
Compare two instances, where "smaller" means earlier in time.Note: because timestamps recycle every 48 days, this method does not totally order instances.
- Specified by:
compareTo
in interfaceComparable<Timestamp>
- Parameters:
that
- timestamp to compare with- Throws:
IllegalArgumentException
- if this instance andthat
differ by exactly 231 millisecondsNullPointerException
- ifthat
is null
-