Class Duration

java.lang.Object
com.ethlo.time.Duration
All Implemented Interfaces:
Comparable<Duration>

public class Duration extends Object implements Comparable<Duration>
Represents a precise duration in seconds and nanoseconds. The nanoseconds field is always positive, with the sign absorbed by the seconds field.
  • Field Details

    • ZERO

      public static final Duration ZERO
    • NANOS_PER_SECOND

      public static final int NANOS_PER_SECOND
      See Also:
    • SECONDS_PER_MINUTE

      public static final long SECONDS_PER_MINUTE
      See Also:
    • SECONDS_PER_HOUR

      public static final long SECONDS_PER_HOUR
      See Also:
    • SECONDS_PER_DAY

      public static final long SECONDS_PER_DAY
      See Also:
    • SECONDS_PER_WEEK

      public static final long SECONDS_PER_WEEK
      See Also:
    • NANOS_PER_MICROSECOND

      public static final long NANOS_PER_MICROSECOND
      See Also:
    • NANOS_PER_MILLISECOND

      public static final long NANOS_PER_MILLISECOND
      See Also:
    • seconds

      private final long seconds
    • nanos

      private final int nanos
  • Constructor Details

    • 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:
      IllegalArgumentException - if nanos is out of range.
  • Method Details

    • 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 String normalized()
      Returns a normalized string representation of this duration.
      Returns:
      The normalized duration string.
    • timeline

      public Instant timeline()
      Computes an Instant that represents this duration on the timeline from now
      Returns:
      The computed instant representing the point on the timeline
    • timeline

      public Instant timeline(Instant instant)
      Computes an Instant 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

      Duration toDuration()
      Converts this duration to a Duration.
      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(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(Duration o)
      Specified by:
      compareTo in interface 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)