Package com.ethlo.time
Class Duration
- java.lang.Object
-
- com.ethlo.time.Duration
-
-
Field Summary
Fields Modifier and Type Field Description private int
nanos
static long
NANOS_PER_MICROSECOND
static long
NANOS_PER_MILLISECOND
static int
NANOS_PER_SECOND
private long
seconds
static long
SECONDS_PER_DAY
static long
SECONDS_PER_HOUR
static long
SECONDS_PER_MINUTE
static long
SECONDS_PER_WEEK
static Duration
ZERO
-
Constructor Summary
Constructors Constructor Description Duration(long seconds, int nanos)
Constructs a duration with the specified seconds and nanoseconds.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Duration
abs()
Returns the absolute durationDuration
add(Duration other)
Adds another duration to this one.int
compareTo(Duration o)
boolean
equals(java.lang.Object object)
int
getNanos()
Returns the nanosecond component of this duration.long
getSeconds()
Returns the number of seconds in this duration.int
hashCode()
private boolean
isNegative()
Duration
negate()
Negates this duration.java.lang.String
normalized()
Returns a normalized string representation of this duration.static Duration
of(long seconds, int nanos)
static Duration
ofDays(long days)
Creates a duration from days.static Duration
ofHours(long hours)
Creates a duration from hours.static Duration
ofMicros(long micros)
Creates a duration from microseconds.static Duration
ofMillis(long millis)
Creates a duration from milliseconds.static Duration
ofMinutes(long minutes)
Creates a duration from minutes.static Duration
ofNanos(long nanos)
Creates a duration from nanoseconds.static Duration
ofSeconds(long seconds)
Creates a duration from seconds.static Duration
ofWeeks(long weeks)
Creates a duration from weeks.Duration
plusHours(long hours)
Duration
plusMinutes(long minutes)
Duration
plusNanos(long nanos)
Duration
plusSeconds(long seconds)
Duration
subtract(Duration other)
Subtracts another duration from this one.java.time.Instant
timeline()
Computes anInstant
that represents this duration on the timeline from nowjava.time.Instant
timeline(java.time.Instant instant)
Computes anInstant
that represents this duration on the timeline from a given instant.(package private) java.time.Duration
toDuration()
Converts this duration to aDuration
.java.lang.String
toString()
-
-
-
Field Detail
-
ZERO
public static final Duration ZERO
-
NANOS_PER_SECOND
public static final int NANOS_PER_SECOND
- See Also:
- Constant Field Values
-
SECONDS_PER_MINUTE
public static final long SECONDS_PER_MINUTE
- See Also:
- Constant Field Values
-
SECONDS_PER_HOUR
public static final long SECONDS_PER_HOUR
- See Also:
- Constant Field Values
-
SECONDS_PER_DAY
public static final long SECONDS_PER_DAY
- See Also:
- Constant Field Values
-
SECONDS_PER_WEEK
public static final long SECONDS_PER_WEEK
- See Also:
- Constant Field Values
-
NANOS_PER_MICROSECOND
public static final long NANOS_PER_MICROSECOND
- See Also:
- Constant Field Values
-
NANOS_PER_MILLISECOND
public static final long NANOS_PER_MILLISECOND
- See Also:
- Constant Field Values
-
seconds
private final long seconds
-
nanos
private final int nanos
-
-
Constructor Detail
-
Duration
Duration(long seconds, int nanos)
Constructs a duration with the specified seconds and nanoseconds.- Parameters:
seconds
- The number of seconds.nanos
- The number of nanoseconds, must be between 0 and 999,999,999.- Throws:
java.lang.IllegalArgumentException
- if nanos is out of range.
-
-
Method Detail
-
ofMillis
public static Duration ofMillis(long millis)
Creates a duration from milliseconds.- Parameters:
millis
- The number of milliseconds.- Returns:
- A corresponding Duration instance.
-
ofMicros
public static Duration ofMicros(long micros)
Creates a duration from microseconds.- Parameters:
micros
- The number of microseconds.- Returns:
- A corresponding Duration instance.
-
ofNanos
public static Duration ofNanos(long nanos)
Creates a duration from nanoseconds.- Parameters:
nanos
- The number of nanoseconds.- Returns:
- A corresponding Duration instance.
-
ofWeeks
public static Duration ofWeeks(long weeks)
Creates a duration from weeks.- Parameters:
weeks
- The number of weeks.- Returns:
- A corresponding Duration instance.
-
ofDays
public static Duration ofDays(long days)
Creates a duration from days.- Parameters:
days
- The number of days.- Returns:
- A corresponding Duration instance.
-
ofHours
public static Duration ofHours(long hours)
Creates a duration from hours.- Parameters:
hours
- The number of hours.- Returns:
- A corresponding Duration instance.
-
ofMinutes
public static Duration ofMinutes(long minutes)
Creates a duration from minutes.- Parameters:
minutes
- The number of minutes.- Returns:
- A corresponding Duration instance.
-
ofSeconds
public static Duration ofSeconds(long seconds)
Creates a duration from seconds.- Parameters:
seconds
- The number of seconds.- Returns:
- A corresponding Duration instance.
-
of
public static Duration of(long seconds, int nanos)
-
getSeconds
public long getSeconds()
Returns the number of seconds in this duration.- Returns:
- The seconds value.
-
getNanos
public int getNanos()
Returns the nanosecond component of this duration.- Returns:
- The nanosecond value.
-
normalized
public java.lang.String normalized()
Returns a normalized string representation of this duration.- Returns:
- The normalized duration string.
-
timeline
public java.time.Instant timeline()
Computes anInstant
that represents this duration on the timeline from now- Returns:
- The computed instant representing the point on the timeline
-
timeline
public java.time.Instant timeline(java.time.Instant instant)
Computes anInstant
that represents this duration on the timeline from a given instant.- Parameters:
instant
- The reference instant.- Returns:
- The computed instant representing the point on the timeline
-
toDuration
java.time.Duration toDuration()
Converts this duration to aDuration
.- Returns:
- The Java Time API equivalent duration.
-
add
public Duration add(Duration other)
Adds another duration to this one.- Parameters:
other
- The duration to add.- Returns:
- A new Duration representing the sum.
-
subtract
public Duration subtract(Duration other)
Subtracts another duration from this one.- Parameters:
other
- The duration to subtract.- Returns:
- A new Duration representing the difference.
-
negate
public Duration negate()
Negates this duration.- Returns:
- A new Duration with the opposite sign.
-
abs
public Duration abs()
Returns the absolute duration- Returns:
- the absolute duration
-
isNegative
private boolean isNegative()
-
equals
public boolean equals(java.lang.Object object)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
compareTo
public int compareTo(Duration o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<Duration>
-
plusHours
public Duration plusHours(long hours)
-
plusMinutes
public Duration plusMinutes(long minutes)
-
plusSeconds
public Duration plusSeconds(long seconds)
-
plusNanos
public Duration plusNanos(long nanos)
-
-