Class TimeStamp
- java.lang.Object
-
- com.metsci.glimpse.util.units.time.TimeStamp
-
- All Implemented Interfaces:
Serializable
,Comparable<TimeStamp>
- Direct Known Subclasses:
TimeStampPosixMicrosInt64
,TimeStampPosixMillisInt64
,TimeStampPosixSecondsFloat64
public abstract class TimeStamp extends Object implements Comparable<TimeStamp>, Serializable
- Author:
- hogye
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static TimeStampFormat
defaultFormat
The format used by the convenience methodsfromString(String)
andtoString()
.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract TimeStamp
add(double duration)
Creates a new TimeStamp instance offset from this timestamp by the given amount.int
compareTo(TimeStamp o)
static TimeStamp
currentTime()
Creates a TimeStamp that represents the current system time.double
durationAfter(TimeStamp o)
Determines how long after the given timestamp this timestamp is.double
durationBefore(TimeStamp o)
Determines how long before the given timestamp this timestamp is.boolean
equals(Object o)
static TimeStamp
fromCalendar(Calendar calendar)
Converts aCalendar
to a TimeStamp.static TimeStamp
fromDate(Date date)
Converts aDate
to a TimeStamp.static TimeStamp
fromPosixMicros(long posixMicros)
Converts a timestamp in posix microseconds to a TimeStamp.static TimeStamp
fromPosixMillis(long posixMillis)
Converts a timestamp in posix milliseconds to a TimeStamp.static TimeStamp
fromPosixNanos(long posixNanos)
Converts a timestamp in posix nanoseconds to a TimeStamp.static TimeStamp
fromPosixSeconds(double posixSeconds)
Converts a timestamp in posix seconds to a TimeStamp.static TimeStamp
fromPosixSeconds(BigDecimal posixSeconds)
Converts a timestamp in posix seconds to a TimeStamp.static TimeStamp
fromString(String string)
Converts aString
to a TimeStamp, usingdefaultFormat
.static TimeStamp
fromString(String string, TimeStampFormat format)
Converts aString
to a TimeStamp.static TimeStamp
fromTimeStamp(TimeStamp timeStamp)
Converts a TimeStamp of any subclass to a TimeStamp of the default subclass.int
hashCode()
boolean
isAfter(TimeStamp o)
Returns true iff this timestamp is after the given timestamp.boolean
isAfterOrEquals(TimeStamp o)
Returns true iff this timestamp is after or equal to the given timestamp.boolean
isBefore(TimeStamp o)
Returns true iff this timestamp is before the given timestamp.boolean
isBeforeOrEquals(TimeStamp o)
Returns true iff this timestamp is before or equal to the given timestamp.static TimeStamp
posixEpoch()
Returns a TimeStamp that represents the posix epoch.abstract TimeStamp
subtract(double duration)
Creates a new TimeStamp instance offset from this timestamp by the given amount.abstract Calendar
toCalendar()
Converts this TimeStamp to aCalendar
.abstract Date
toDate()
Converts this TimeStamp to aDate
.abstract long
toPosixMicros()
Converts this TimeStamp to posix microseconds.abstract long
toPosixMillis()
Converts this TimeStamp to posix milliseconds.abstract long
toPosixNanos()
Converts this TimeStamp to posix nanoseconds.abstract double
toPosixSeconds()
Converts this TimeStamp to posix seconds.abstract BigDecimal
toPosixSecondsExact()
Converts this TimeStamp to posix seconds.String
toString()
Converts this timestamp to aString
, according todefaultFormat
.String
toString(TimeStampFormat format)
Converts this timestamp to aString
, according to the given format.
-
-
-
Field Detail
-
defaultFormat
public static final TimeStampFormat defaultFormat
The format used by the convenience methodsfromString(String)
andtoString()
. It refers toTimeStampFormat.iso8601
.
-
-
Method Detail
-
fromPosixSeconds
public static TimeStamp fromPosixSeconds(double posixSeconds)
Converts a timestamp in posix seconds to a TimeStamp.- Parameters:
posixSeconds
- seconds since the epoch
-
fromPosixSeconds
public static TimeStamp fromPosixSeconds(BigDecimal posixSeconds)
Converts a timestamp in posix seconds to a TimeStamp.- Parameters:
posixSeconds
- seconds since the epoch
-
fromPosixMillis
public static TimeStamp fromPosixMillis(long posixMillis)
Converts a timestamp in posix milliseconds to a TimeStamp.- Parameters:
posixMillis
- milliseconds since the epoch
-
fromPosixMicros
public static TimeStamp fromPosixMicros(long posixMicros)
Converts a timestamp in posix microseconds to a TimeStamp.- Parameters:
posixMicros
- microseconds since the epoch
-
fromPosixNanos
public static TimeStamp fromPosixNanos(long posixNanos)
Converts a timestamp in posix nanoseconds to a TimeStamp.- Parameters:
posixNanos
- nanoseconds since the epoch
-
fromTimeStamp
public static TimeStamp fromTimeStamp(TimeStamp timeStamp)
Converts a TimeStamp of any subclass to a TimeStamp of the default subclass.- Throws:
NullPointerException
- if argument is null
-
fromDate
public static TimeStamp fromDate(Date date)
Converts aDate
to a TimeStamp.- Throws:
NullPointerException
- if argument is null
-
fromCalendar
public static TimeStamp fromCalendar(Calendar calendar)
Converts aCalendar
to a TimeStamp.- Throws:
NullPointerException
- if argument is null
-
fromString
public static TimeStamp fromString(String string, TimeStampFormat format) throws TimeStampParseException
Converts aString
to a TimeStamp.- Throws:
NullPointerException
- if either argument is nullTimeStampParseException
- if string parsing fails
-
fromString
public static TimeStamp fromString(String string) throws TimeStampParseException
Converts aString
to a TimeStamp, usingdefaultFormat
.- Throws:
NullPointerException
- if either argument is nullTimeStampParseException
- if string parsing fails
-
posixEpoch
public static TimeStamp posixEpoch()
Returns a TimeStamp that represents the posix epoch.
-
currentTime
public static TimeStamp currentTime()
Creates a TimeStamp that represents the current system time. The precision may be limited by the precision of Java's access to the system clock.
-
add
public abstract TimeStamp add(double duration)
Creates a new TimeStamp instance offset from this timestamp by the given amount. The returned instance may or may not be of the same dynamic type as this instance.- Parameters:
duration
- the amount of time to add in system-units
-
subtract
public abstract TimeStamp subtract(double duration)
Creates a new TimeStamp instance offset from this timestamp by the given amount. The returned instance may or may not be of the same dynamic type as this instance.- Parameters:
duration
- the amount of time to subtract in system-units
-
durationBefore
public double durationBefore(TimeStamp o)
Determines how long before the given timestamp this timestamp is. That is: durationBefore = other - this In the base-class implementation, arithmetic is done with BigDecimal. This is quite effective at minimizing rounding error, but may be slow compared to primitive arithmetic.- Returns:
- the difference, in system-units
- Throws:
NullPointerException
- if argument is null
-
durationAfter
public double durationAfter(TimeStamp o)
Determines how long after the given timestamp this timestamp is. That is: durationAfter = this - other In the base-class implementation, arithmetic is done with BigDecimal. This is quite effective at minimizing rounding error, but may be slow compared to primitive arithmetic.- Returns:
- the difference, in system-units
- Throws:
NullPointerException
- if argument is null
-
toPosixSeconds
public abstract double toPosixSeconds()
Converts this TimeStamp to posix seconds.- Returns:
- seconds since the epoch
-
toPosixMillis
public abstract long toPosixMillis()
Converts this TimeStamp to posix milliseconds.- Returns:
- milliseconds since the epoch
-
toPosixMicros
public abstract long toPosixMicros()
Converts this TimeStamp to posix microseconds.- Returns:
- microseconds since the epoch
-
toPosixNanos
public abstract long toPosixNanos()
Converts this TimeStamp to posix nanoseconds.- Returns:
- nanoseconds since the epoch
-
toPosixSecondsExact
public abstract BigDecimal toPosixSecondsExact()
Converts this TimeStamp to posix seconds. The returned representation is exact (see note). NOTE: In theory, it is possible to have a subclass that stores its value in a form that cannot be exactly converted to a BigDecimal (e.g., thirds of a second). Such subclasses are discouraged. If such a subclass is unavoidable, its documentation must note prominently that it breaks the contract of the TimeStamp interface, and its implementation of this method must return values as exact as is practical.- Returns:
- seconds since the epoch
-
isBefore
public boolean isBefore(TimeStamp o)
Returns true iff this timestamp is before the given timestamp.- Throws:
NullPointerException
- if argument is null
-
isBeforeOrEquals
public boolean isBeforeOrEquals(TimeStamp o)
Returns true iff this timestamp is before or equal to the given timestamp.- Throws:
NullPointerException
- if argument is null
-
isAfter
public boolean isAfter(TimeStamp o)
Returns true iff this timestamp is after the given timestamp.- Throws:
NullPointerException
- if argument is null
-
isAfterOrEquals
public boolean isAfterOrEquals(TimeStamp o)
Returns true iff this timestamp is after or equal to the given timestamp.- Throws:
NullPointerException
- if argument is null
-
compareTo
public int compareTo(TimeStamp o)
- Specified by:
compareTo
in interfaceComparable<TimeStamp>
-
toString
public String toString(TimeStampFormat format)
Converts this timestamp to aString
, according to the given format.
-
toString
public String toString()
Converts this timestamp to aString
, according todefaultFormat
.
-
-