Class Duration


  • public final class Duration
    extends java.lang.Object
    Represents a duration. A durations store 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 Detail

      • newInstance

        public static Duration newInstance​(int months,
                                           int days,
                                           long nanoseconds)
      • 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 number of nanoseconds
      • getMonths

        public int getMonths()
      • getDays

        public int getDays()
      • getNanoseconds

        public long getNanoseconds()
      • addTo

        public long addTo​(long timeInMillis)
        Adds this duration to the specified time in milliseconds.
        Parameters:
        timeInMillis - the time to which the duration must be added
        Returns:
        the specified time plus this duration
      • substractFrom

        public long substractFrom​(long timeInMillis)
        Substracts this duration from the specified time in milliseconds.
        Parameters:
        timeInMillis - the time from which the duration must be substracted
        Returns:
        the specified time minus 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
      • hasDayPrecision

        public boolean hasDayPrecision()
        Checks if that duration has a day precision (nothing bellow the day level).
        Returns:
        true if that duration has a day precision, false otherwise
      • hasMillisecondPrecision

        public boolean hasMillisecondPrecision()
        Checks if that duration has a millisecond precision (nothing bellow the millisecond level).
        Returns:
        true if that duration has a millisecond precision, false otherwise
      • floorTimestamp

        public static long floorTimestamp​(long timeInMillis,
                                          Duration duration,
                                          long startingTimeInMillis)
        Rounds a timestamp down to the closest multiple of a duration.
        Parameters:
        timeInMillis - the time to round in millisecond
        duration - the duration
        startingTimeInMillis - the time offset in milliseconds
        Returns:
        the timestamp rounded down to the closest multiple of the duration
      • floorTime

        public static long floorTime​(long timeInNanos,
                                     Duration duration)
        Rounds a time down to the closest multiple of a duration.
        Parameters:
        timeInNanos - the time of day in nanoseconds
        duration - the duration
        Returns:
        the time rounded down to the closest multiple of the duration
      • isNegative

        public boolean isNegative()
        Checks if the duration is negative.
        Returns:
        true if the duration is negative, false otherwise