Class Duration

  • All Implemented Interfaces:
    java.lang.Comparable<Duration>

    public class Duration
    extends java.lang.Object
    implements java.lang.Comparable<Duration>
    Represents a precise duration in seconds and nanoseconds. The nanoseconds field is always positive, with the sign absorbed by the seconds field.
    • 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 an Instant 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 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

        java.time.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​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

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

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