Package com.ethlo.time
Class Duration
- java.lang.Object
-
- com.ethlo.time.Duration
-
-
Field Summary
Fields Modifier and Type Field Description private intnanosstatic longNANOS_PER_MICROSECONDstatic longNANOS_PER_MILLISECONDstatic intNANOS_PER_SECONDprivate longsecondsstatic longSECONDS_PER_DAYstatic longSECONDS_PER_HOURstatic longSECONDS_PER_MINUTEstatic longSECONDS_PER_WEEKstatic DurationZERO
-
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 Durationabs()Returns the absolute durationDurationadd(Duration other)Adds another duration to this one.intcompareTo(Duration o)booleanequals(java.lang.Object object)intgetNanos()Returns the nanosecond component of this duration.longgetSeconds()Returns the number of seconds in this duration.inthashCode()private booleanisNegative()Durationnegate()Negates this duration.java.lang.Stringnormalized()Returns a normalized string representation of this duration.static Durationof(long seconds, int nanos)static DurationofDays(long days)Creates a duration from days.static DurationofHours(long hours)Creates a duration from hours.static DurationofMicros(long micros)Creates a duration from microseconds.static DurationofMillis(long millis)Creates a duration from milliseconds.static DurationofMinutes(long minutes)Creates a duration from minutes.static DurationofNanos(long nanos)Creates a duration from nanoseconds.static DurationofSeconds(long seconds)Creates a duration from seconds.static DurationofWeeks(long weeks)Creates a duration from weeks.DurationplusHours(long hours)DurationplusMinutes(long minutes)DurationplusNanos(long nanos)DurationplusSeconds(long seconds)Durationsubtract(Duration other)Subtracts another duration from this one.java.time.Instanttimeline()Computes anInstantthat represents this duration on the timeline from nowjava.time.Instanttimeline(java.time.Instant instant)Computes anInstantthat represents this duration on the timeline from a given instant.(package private) java.time.DurationtoDuration()Converts this duration to aDuration.java.lang.StringtoString()
-
-
-
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 anInstantthat 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 anInstantthat 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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
compareTo
public int compareTo(Duration o)
- Specified by:
compareToin 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)
-
-