Class Duration

  • All Implemented Interfaces:
    java.lang.Comparable<Duration>
    Direct Known Subclasses:
    AutoValue_Duration

    @Immutable
    public abstract class Duration
    extends java.lang.Object
    implements java.lang.Comparable<Duration>
    Represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day" or "month". Range is approximately +-10,000 years.
    Since:
    0.5
    • Constructor Summary

      Constructors 
      Constructor Description
      Duration()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Duration otherDuration)
      Compares this Duration to the specified Duration.
      static Duration create​(long seconds, int nanos)
      Creates a new time duration from given seconds and nanoseconds.
      static Duration fromMillis​(long millis)
      Creates a new Duration from given milliseconds.
      abstract int getNanos()
      Returns the number of nanoseconds in the Duration.
      abstract long getSeconds()
      Returns the number of seconds in the Duration.
      long toMillis()
      Converts a Duration to milliseconds.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Duration

        Duration()
    • Method Detail

      • create

        public static Duration create​(long seconds,
                                      int nanos)
        Creates a new time duration from given seconds and nanoseconds.
        Parameters:
        seconds - Signed seconds of the span of time. Must be from -315,576,000,000 to +315,576,000,000 inclusive.
        nanos - Signed fractions of a second at nanosecond resolution of the span of time. Durations less than one second are represented with a 0 `seconds` field and a positive or negative `nanos` field. For durations of one second or more, a non-zero value for the `nanos` field must be of the same sign as the `seconds` field. Must be from -999,999,999 to +999,999,999 inclusive.
        Returns:
        new Duration with specified fields.
        Throws:
        java.lang.IllegalArgumentException - if the arguments are out of range or have inconsistent sign.
        Since:
        0.5
      • fromMillis

        public static Duration fromMillis​(long millis)
        Creates a new Duration from given milliseconds.
        Parameters:
        millis - the duration in milliseconds.
        Returns:
        a new Duration from given milliseconds.
        Throws:
        java.lang.IllegalArgumentException - if the number of milliseconds is out of the range that can be represented by Duration.
        Since:
        0.5
      • toMillis

        public long toMillis()
        Converts a Duration to milliseconds.
        Returns:
        the milliseconds representation of this Duration.
        Since:
        0.13
      • getSeconds

        public abstract long getSeconds()
        Returns the number of seconds in the Duration.
        Returns:
        the number of seconds in the Duration.
        Since:
        0.5
      • getNanos

        public abstract int getNanos()
        Returns the number of nanoseconds in the Duration.
        Returns:
        the number of nanoseconds in the Duration.
        Since:
        0.5
      • compareTo

        public int compareTo​(Duration otherDuration)
        Compares this Duration to the specified Duration.
        Specified by:
        compareTo in interface java.lang.Comparable<Duration>
        Parameters:
        otherDuration - the other Duration to compare to, not null.
        Returns:
        the comparator value: zero if equal, negative if this duration is smaller than otherDuration, positive if larger.
        Throws:
        java.lang.NullPointerException - if otherDuration is null.