Class TimeSpan

  • All Implemented Interfaces:
    Serializable, Comparable<TimeSpan>, TemporalAmount

    @Immutable
    public final class TimeSpan
    extends Object
    implements Comparable<TimeSpan>, Serializable, TemporalAmount
    This is an immutable representation of some time span, using a TimeUnit and a value.

    The value may be positive or negative. All operations check for overflows and underflows, the behavior on overflow and underflow differs and is documented for each method.

    Two instances are considered equal if they represent the exact same time span regardless of their unit, for example, 60s and 1min are considered equal.

    See Also:
    Serialized Form
    • Method Detail

      • ofSeconds

        public static TimeSpan ofSeconds​(long pSeconds)
      • ofMillis

        public static TimeSpan ofMillis​(long pMillis)
      • ofNanos

        public static TimeSpan ofNanos​(long pNanos)
      • empty

        public static TimeSpan empty()
      • getChecked

        public long getChecked​(TimeUnit dest)
        Get the value of this TimeSpan represented in the given unit. If the given unit is larger than the current unit, precision may be lost.
        Throws:
        ArithmeticException - If the value cannot be represented in the given unit due to overflow.
      • getSaturated

        public long getSaturated​(TimeUnit dest)
        Get the value of this TimeSpan represented in the given unit. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, Long.MAX_VALUE/Long.MIN_VALUE is returned.
      • toChecked

        public TimeSpan toChecked​(TimeUnit dest)
        Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit. If the given unit is larger than the current unit, precision may be lost.
        Throws:
        ArithmeticException - If the value cannot be represented in the given unit
      • toSaturated

        public TimeSpan toSaturated​(TimeUnit dest)
        Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, Long.MAX_VALUE/Long.MIN_VALUE is returned.
      • asSeconds

        public long asSeconds()
        Get the value of this TimeSpan as seconds. If the current unit is smaller than seconds, precision may be lost.
        Throws:
        ArithmeticException - If the value cannot be represented as seconds due to overflow.
      • asMillis

        public long asMillis()
        Get the value of this TimeSpan as milliseconds. If the current unit is smaller than milliseconds, precision may be lost.
        Throws:
        ArithmeticException - If the value cannot be represented as milliseconds due to overflow.
      • asNanos

        public long asNanos()
        Get the value of this TimeSpan as nanoseconds.
        Throws:
        ArithmeticException - If the value cannot be represented as nanoseconds due to overflow.
      • asDuration

        public Duration asDuration()
        Return a Duration instance that represents the same amount of time (but it won't be possible to retrieve our unit as from getUnit()). Note that not all possible TimeSpan values can be represented by a Duration.
        Throws:
        ArithmeticException - If an overflow occurs.
      • formatAs

        public String formatAs​(TimeUnit dest)
        Return a strings that represents (approximately) this time span, in the given unit if possible. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, the result does not use the given unit, but the closest unit one that still allows to hold the exact value.
      • isEmpty

        public boolean isEmpty()
        Check whether this time span is empty, i.e., represents 0ns (or 0ms or 0s or ...).
      • equals

        public boolean equals​(@Nullable Object obj)
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • sum

        public static TimeSpan sum​(TimeSpan a,
                                   TimeSpan b)
        Create a new time span that is the sum of two time spans. The unit of the returned time span is the more precise one if possible, otherwise the closest unit that still allows to hold both input values and the result. Note that this can loose precision when adding a very large and a very small value.
        Throws:
        ArithmeticException - If no unit is large enough to represent the result value.
      • sum

        public static TimeSpan sum​(Iterable<TimeSpan> timeSpans)
        Create a new time span that is the sum of several time spans. The unit of the returned time span is the most precise one if possible, otherwise the closest unit that still allows to hold input values and the result. Note that this can loose precision when adding very large and very small values.
        Throws:
        ArithmeticException - If no unit is large enough to represent the result value.
      • sum

        public static TimeSpan sum​(TimeSpan... t)
        Create a new time span that is the sum of several time spans. The unit of the returned time span is the most precise one.
      • difference

        public static TimeSpan difference​(TimeSpan a,
                                          TimeSpan b)
        Create a new time span that is the difference of two time spans. The unit of the returned time span is the more precise one if possible, otherwise the closest unit that still allows to hold both input values and the result. Note that this can loose precision when subtracting a very large and a very small value.
      • multiply

        @CheckReturnValue
        public TimeSpan multiply​(int factor)
        Create a new time span that is the current one multiplied by a non-negative integral factor. The unit of the returned time span is the same as the current one if possible, otherwise the closest unit that still allows to the result. Note that this can loose precision.
      • divide

        @CheckReturnValue
        public TimeSpan divide​(int divisor)
        Create a new time span that is the current one divided by a non-negative integral value. The result of the division is rounded down (integer division). The unit of the returned time span is the same as the current one.