public class Timestamp extends Object implements 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.
| Modifier and Type | Field and Description |
|---|---|
static Comparator<Timestamp> |
NULL_FIRST_SORT
Sorts possibly null
Timestamps in chronological order, with null sorting first. |
| Constructor and Description |
|---|
Timestamp()
Construtor returning the current time.
|
Timestamp(int millis)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(Timestamp that)
Compare two instances, where "smaller" means earlier in time.
|
boolean |
equals(Object obj) |
int |
getMillis()
Get the relative milliseconds value contained by this instance.
|
int |
hashCode() |
boolean |
hasOccurred()
Determine whether this timestamp is in the past or the future.
|
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.
|
Timestamp |
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 |
offsetFromNow()
Get the number of milliseconds this instance is offset from the current time.
|
String |
toString() |
public static final Comparator<Timestamp> NULL_FIRST_SORT
public Timestamp()
public Timestamp(int millis)
millis - relative milliseconds value from getMillis()public int getMillis()
public int offsetFromNow()
A positive offset means this instance is in the future.
public int offsetFrom(Timestamp base)
A positive offset means this instance is after base.
base - base timestampIllegalArgumentException - if base is nullpublic Timestamp offset(int offset)
offset - offset in millisecondspublic boolean hasOccurred()
public boolean isRolloverDanger()
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.
public int compareTo(Timestamp that)
Note: because timestamps recycle every 48 days, this method does not totally order instances.
compareTo in interface Comparable<Timestamp>that - timestamp to compare withIllegalArgumentException - if this instance and that differ by exactly 231 millisecondsNullPointerException - if that is nullCopyright © 2016. All rights reserved.