public class TimeDuration extends Object implements Comparable<TimeDuration>
Timestamp
class.
This class can be used both to represent a span of time, or as a relative timestamp (e.g. 3 seconds before or after a reference). As such, it allows for "negative" durations. Even for negative values the nanoseconds is positive. For example, -1.5 seconds will be stored as -2 seconds and 500,000,000 nanoseconds.
Note that while Timestamp are usually created according to system clocks which takes into account leap seconds, all the math operations on TimeStamps do not take leap seconds into account.
javax.time.Duration
.
When it will be released, the plan is to phase out this class where appropriate.Modifier and Type | Method and Description |
---|---|
TimeInterval |
after(Timestamp reference)
Returns a time interval that lasts this duration and starts from the
given timestamp.
|
TimeInterval |
around(Timestamp reference)
Returns a time interval that lasts this duration and is centered
around the given timestamp.
|
TimeInterval |
before(Timestamp reference)
Returns a time interval that lasts this duration and ends at the
given timestamp.
|
int |
compareTo(TimeDuration other) |
TimeDuration |
dividedBy(int factor)
Returns a new duration which is smaller by the given factor.
|
int |
dividedBy(TimeDuration duration)
Returns the number of times the given duration is present in this duration.
|
boolean |
equals(Object obj) |
int |
getNanoSec()
The amount of nanoseconds for the duration.
|
long |
getSec()
The amount of seconds for the duration.
|
int |
hashCode() |
boolean |
isNegative()
True if the duration is non-zero and negative.
|
boolean |
isPositive()
True if the duration is non-zero and positive.
|
TimeDuration |
minus(TimeDuration duration)
Returns the difference between this duration and the given.
|
TimeDuration |
multipliedBy(int factor)
Returns a new duration which is bigger by the given factor.
|
static TimeDuration |
ofHertz(double hz)
A new duration in hertz, will convert to the length of the period.
|
static TimeDuration |
ofHours(double hour)
A new duration in hours.
|
static TimeDuration |
ofMillis(int ms)
A new duration in milliseconds.
|
static TimeDuration |
ofMinutes(double min)
A new duration in minutes.
|
static TimeDuration |
ofNanos(long nanoSec)
A new duration in nanoseconds.
|
static TimeDuration |
ofSeconds(double sec)
A new duration in seconds.
|
TimeDuration |
plus(TimeDuration duration)
Returns the sum of this duration with the given.
|
long |
toNanosLong()
Returns the duration in nanoseconds.
|
double |
toSeconds()
Returns the duration in seconds.
|
String |
toString()
The number of seconds concatenated with the number of nanoseconds (12.500000000
for 12.5 seconds).
|
public boolean isPositive()
public boolean isNegative()
public int getNanoSec()
public long getSec()
public static TimeDuration ofHours(double hour)
hour
- hourspublic static TimeDuration ofMinutes(double min)
min
- minutespublic static TimeDuration ofSeconds(double sec)
sec
- secondspublic static TimeDuration ofHertz(double hz)
hz
- frequency to be converted to a durationpublic static TimeDuration ofMillis(int ms)
ms
- milliseconds of the durationpublic static TimeDuration ofNanos(long nanoSec)
nanoSec
- nanoseconds of the durationIllegalArgumentException
- if the duration is negativepublic TimeDuration dividedBy(int factor)
factor
- constant to dividepublic int dividedBy(TimeDuration duration)
duration
- another durationpublic TimeDuration multipliedBy(int factor)
factor
- constant to multiplypublic TimeDuration plus(TimeDuration duration)
duration
- another durationpublic TimeDuration minus(TimeDuration duration)
duration
- another durationpublic TimeInterval around(Timestamp reference)
reference
- a timestamppublic TimeInterval after(Timestamp reference)
reference
- a timestamppublic TimeInterval before(Timestamp reference)
reference
- a timestamppublic long toNanosLong()
The maximum duration is years is 2^63 / 1,000,000,000 / 60 / 60 / 24 / 365 = about 292 years. Which is safe for many many cases.
public double toSeconds()
public String toString()
public int compareTo(TimeDuration other)
compareTo
in interface Comparable<TimeDuration>
Copyright © 2015. All rights reserved.