Class Duration


  • public final class Duration
    extends java.lang.Object
    Represents a duration. A duration stores separately months, days, and seconds due to the fact that the number of days in a month varies, and a day can have 23 or 25 hours if a daylight saving is involved.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      static Duration from​(java.lang.String input)
      Converts a String into a duration.
      int getDays()
      Returns the number of days in this duration.
      int getMonths()
      Returns the number of months in this duration.
      long getNanoseconds()
      Returns the number of nanoseconds in this duration.
      int hashCode()  
      static Duration newInstance​(int months, int days, long nanoseconds)
      Creates a duration with the given number of months, days and nanoseconds.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • newInstance

        public static Duration newInstance​(int months,
                                           int days,
                                           long nanoseconds)
        Creates a duration with the given number of months, days and nanoseconds.

        A duration can be negative. In this case, all the non zero values must be negative.

        Parameters:
        months - the number of months
        days - the number of days
        nanoseconds - the number of nanoseconds
        Throws:
        java.lang.IllegalArgumentException - if the values are not all negative or all positive
      • from

        public static Duration from​(java.lang.String input)
        Converts a String into a duration.

        The accepted formats are:

        • multiple digits followed by a time unit like: 12h30m where the time unit can be:
          • y: years
          • m: months
          • w: weeks
          • d: days
          • h: hours
          • m: minutes
          • s: seconds
          • ms: milliseconds
          • us or µs: microseconds
          • ns: nanoseconds
        • ISO 8601 format: P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W
        • ISO 8601 alternative format: P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]
        Parameters:
        input - the String to convert
        Returns:
        a Duration
      • getMonths

        public int getMonths()
        Returns the number of months in this duration.
        Returns:
        the number of months in this duration.
      • getDays

        public int getDays()
        Returns the number of days in this duration.
        Returns:
        the number of days in this duration.
      • getNanoseconds

        public long getNanoseconds()
        Returns the number of nanoseconds in this duration.
        Returns:
        the number of months in this duration.
      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

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