Class Duration

  • All Implemented Interfaces:
    Comparable<Duration>

    public class Duration
    extends Object
    implements Comparable<Duration>
    Represents a duration in english. Cases is not important, plurals units are accepted. Notice that negative durations are not supported. 6 days 59 minutes and 1 millisecond 1 minute and 10 seconds 42 millis unlimited none zero
    • Field Detail

      • UNLIMITED

        public static final Duration UNLIMITED
        Special duration that represents an unlimited duration (or indefinite).
      • ZERO

        public static final Duration ZERO
        Special duration that represents a zero-length duration.
    • Constructor Detail

      • Duration

        @Deprecated
        public Duration​(Long number,
                        TimeUnit unit)
        Deprecated.
        Prefer the use of duration(long, TimeUnit).
        Builds a new Duration.
        Parameters:
        number - number of time unit (cannot be null).
        unit - TimeUnit to express the duration in (cannot be null).
    • Method Detail

      • duration

        public static Duration duration​(long number,
                                        TimeUnit unit)
        Provides a Duration, given a number and time unit.
        Parameters:
        number - number of time unit.
        unit - TimeUnit to express the duration in (cannot be null).
        Returns:
        Duration instance
      • duration

        public static Duration duration​(String value)
        Provides a Duration that represents the given duration expressed in english.
        Parameters:
        value - natural speech duration
        Returns:
        Duration instance
        Throws:
        IllegalArgumentException - if the input string is incorrectly formatted.
      • getValue

        public long getValue()
        Returns the number of TimeUnit this duration represents.
        Returns:
        the number of TimeUnit this duration represents.
      • toJavaDuration

        public Duration toJavaDuration()
        Returns this duration as a Java Duration.
        Returns:
        a new equivalent Java Duration
      • getUnit

        public TimeUnit getUnit()
        Returns the TimeUnit this duration is expressed in.
        Returns:
        the TimeUnit this duration is expressed in.
      • convertTo

        public Duration convertTo​(TimeUnit targetUnit)
        Convert the current duration to a given TimeUnit. Conversions from finer to coarser granularities truncate, so loose precision.
        Parameters:
        targetUnit - target unit of the conversion.
        Returns:
        converted duration
        See Also:
        TimeUnit.convert(long, TimeUnit)
      • to

        public long to​(TimeUnit targetUnit)
        Convert the current duration to a number of given TimeUnit. Conversions from finer to coarser granularities truncate, so loose precision.
        Parameters:
        targetUnit - target unit of the conversion.
        Returns:
        converted duration value
        See Also:
        TimeUnit.convert(long, TimeUnit)
      • isUnlimited

        public boolean isUnlimited()
        Returns true if this Duration represents an unlimited (or indefinite) duration.
        Returns:
        true if this Duration represents an unlimited duration.
      • isZero

        public boolean isZero()
        Returns true if this Duration represents a zero-length duration.
        Returns:
        true if this Duration represents a zero-length duration.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object