org.threeten.bp

Duration

final class Duration extends TemporalAmount with Ordered[Duration] with Serializable

A time-based amount of time, such as '34.5 seconds'.

This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours. In addition, the ChronoUnit#DAYS DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects. See Period for the date-based equivalent to this class.

A physical duration could be of infinite length. For practicality, the duration is stored with constraints similar to Instant. The duration uses nanosecond resolution with a maximum value of the seconds that can be held in a long. This is greater than the current estimated age of the universe.

The range of a duration requires the storage of a number larger than a long. To achieve this, the class stores a long representing seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999.

The duration is measured in "seconds", but these are not necessarily identical to the scientific "SI second" definition based on atomic clocks. This difference only impacts durations measured near a leap-second and should not affect most applications. See Instant for a discussion as to the meaning of the second and time-scales.

Specification for implementors

This class is immutable and thread-safe.

Constructs an instance of Duration using seconds and nanoseconds.

Annotations
@SerialVersionUID( 3078945930695997490L )
Linear Supertypes
Serializable, Ordered[Duration], Comparable[Duration], TemporalAmount, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Duration
  2. Serializable
  3. Ordered
  4. Comparable
  5. TemporalAmount
  6. AnyRef
  7. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. def <(that: Duration): Boolean

    Definition Classes
    Ordered
  5. def <=(that: Duration): Boolean

    Definition Classes
    Ordered
  6. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  8. def >(that: Duration): Boolean

    Definition Classes
    Ordered
  9. def >=(that: Duration): Boolean

    Definition Classes
    Ordered
  10. def abs: Duration

    Returns a copy of this duration with a positive length.

    Returns a copy of this duration with a positive length.

    This method returns a positive duration by effectively removing the sign from any negative total length. For example, PT-1.3S will be returned as PT1.3S.

    This instance is immutable and unaffected by this method call.

    returns

    a { @code Duration} based on this duration with an absolute length, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  11. def addTo(temporal: Temporal): Temporal

    Adds this duration to the specified temporal object.

    Adds this duration to the specified temporal object.

    This returns a temporal object of the same observable type as the input with this duration added.

    In most cases, it is clearer to reverse the calling pattern by using Temporal#plus(TemporalAmount).

    // these two lines are equivalent, but the second approach is recommended
    dateTime = thisDuration.addTo(dateTime);
    dateTime = dateTime.plus(thisDuration);
    

    The calculation will add the seconds, then nanos. Only non-zero amounts will be added.

    This instance is immutable and unaffected by this method call.

    temporal

    the temporal object to adjust, not null

    returns

    an object of the same type with the adjustment made, not null

    Definition Classes
    DurationTemporalAmount
    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

    DateTimeException

    if unable to add

  12. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  13. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  14. def compare(otherDuration: Duration): Int

    Compares this duration to the specified Duration.

    Compares this duration to the specified Duration.

    The comparison is based on the total length of the durations. It is "consistent with equals", as defined by Comparable.

    otherDuration

    the other duration to compare to, not null

    returns

    the comparator value, negative if less, positive if greater

    Definition Classes
    Duration → Ordered
  15. def compareTo(other: Duration): Int

    Definition Classes
    Duration → Ordered → Comparable
  16. def dividedBy(divisor: Long): Duration

    Returns a copy of this duration divided by the specified value.

    Returns a copy of this duration divided by the specified value.

    This instance is immutable and unaffected by this method call.

    divisor

    the value to divide the duration by, positive or negative, not zero

    returns

    a { @code Duration} based on this duration divided by the specified divisor, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  17. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  18. def equals(other: Any): Boolean

    Checks if this duration is equal to the specified Duration.

    Checks if this duration is equal to the specified Duration.

    The comparison is based on the total length of the durations.

    other

    the other duration, null returns false

    returns

    true if the other duration is equal to this one

    Definition Classes
    Duration → AnyRef → Any
  19. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  20. def get(unit: TemporalUnit): Long

    Gets the amount associated with the specified unit.

    Gets the amount associated with the specified unit.

    unit

    the unit to get, not null

    returns

    the amount of the unit

    Definition Classes
    DurationTemporalAmount
    Exceptions thrown
    DateTimeException

    if the amount cannot be obtained

  21. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  22. def getNano: Int

    Gets the number of nanoseconds within the second in this duration.

    Gets the number of nanoseconds within the second in this duration.

    The length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total duration is defined by calling this method and #getSeconds().

    A Duration represents a directed distance between two points on the time-line. A negative duration is expressed by the negative sign of the seconds part. A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.

    returns

    the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999

  23. def getSeconds: Long

    Gets the number of seconds in this duration.

    Gets the number of seconds in this duration.

    The length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total duration is defined by calling this method and #getNano().

    A Duration represents a directed distance between two points on the time-line. A negative duration is expressed by the negative sign of the seconds part. A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.

    returns

    the whole seconds part of the length of the duration, positive or negative

  24. def getUnits: List[TemporalUnit]

    Gets the list of units, from largest to smallest, that fully define this amount.

    Gets the list of units, from largest to smallest, that fully define this amount.

    returns

    the list of units.

    Definition Classes
    DurationTemporalAmount
  25. def hashCode(): Int

    A hash code for this duration.

    A hash code for this duration.

    returns

    a suitable hash code

    Definition Classes
    Duration → AnyRef → Any
  26. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  27. def isNegative: Boolean

    Checks if this duration is negative, excluding zero.

    Checks if this duration is negative, excluding zero.

    A Duration represents a directed distance between two points on the time-line and can therefore be positive, zero or negative. This method checks whether the length is less than zero.

    returns

    true if this duration has a total length less than zero

  28. def isZero: Boolean

    Checks if this duration is zero length.

    Checks if this duration is zero length.

    A Duration represents a directed distance between two points on the time-line and can therefore be positive, zero or negative. This method checks whether the length is zero.

    returns

    true if this duration has a total length equal to zero

  29. def minus(amountToSubtract: Long, unit: TemporalUnit): Duration

    Returns a copy of this duration with the specified duration subtracted.

    Returns a copy of this duration with the specified duration subtracted.

    The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by this method. The unit must either have an TemporalUnit#isDurationEstimated() exact duration or be ChronoUnit#DAYS which is treated as 24 hours. Other units throw an exception.

    This instance is immutable and unaffected by this method call.

    amountToSubtract

    the amount of the period, measured in terms of the unit, positive or negative

    unit

    the unit that the period is measured in, must have an exact duration, not null

    returns

    a { @code Duration} based on this duration with the specified duration subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  30. def minus(duration: Duration): Duration

    Returns a copy of this duration with the specified duration subtracted.

    Returns a copy of this duration with the specified duration subtracted.

    This instance is immutable and unaffected by this method call.

    duration

    the duration to subtract, positive or negative, not null

    returns

    a { @code Duration} based on this duration with the specified duration subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  31. def minusDays(daysToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in 24 hour days subtracted.

    Returns a copy of this duration with the specified duration in 24 hour days subtracted.

    This instance is immutable and unaffected by this method call.

    daysToSubtract

    the days to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified days subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  32. def minusHours(hoursToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in hours subtracted.

    Returns a copy of this duration with the specified duration in hours subtracted.

    This instance is immutable and unaffected by this method call.

    hoursToSubtract

    the hours to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified hours subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  33. def minusMillis(millisToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in milliseconds subtracted.

    Returns a copy of this duration with the specified duration in milliseconds subtracted.

    This instance is immutable and unaffected by this method call.

    millisToSubtract

    the milliseconds to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified milliseconds subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  34. def minusMinutes(minutesToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in minutes subtracted.

    Returns a copy of this duration with the specified duration in minutes subtracted.

    This instance is immutable and unaffected by this method call.

    minutesToSubtract

    the minutes to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified minutes subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  35. def minusNanos(nanosToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in nanoseconds subtracted.

    Returns a copy of this duration with the specified duration in nanoseconds subtracted.

    This instance is immutable and unaffected by this method call.

    nanosToSubtract

    the nanoseconds to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified nanoseconds subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  36. def minusSeconds(secondsToSubtract: Long): Duration

    Returns a copy of this duration with the specified duration in seconds subtracted.

    Returns a copy of this duration with the specified duration in seconds subtracted.

    This instance is immutable and unaffected by this method call.

    secondsToSubtract

    the seconds to subtract, positive or negative

    returns

    a { @code Duration} based on this duration with the specified seconds subtracted, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  37. def multipliedBy(multiplicand: Long): Duration

    Returns a copy of this duration multiplied by the scalar.

    Returns a copy of this duration multiplied by the scalar.

    This instance is immutable and unaffected by this method call.

    multiplicand

    the value to multiply the duration by, positive or negative

    returns

    a { @code Duration} based on this duration multiplied by the specified scalar, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  38. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  39. def negated: Duration

    Returns a copy of this duration with the length negated.

    Returns a copy of this duration with the length negated.

    This method swaps the sign of the total length of this duration. For example, PT1.3S will be returned as PT-1.3S.

    This instance is immutable and unaffected by this method call.

    returns

    a { @code Duration} based on this duration with the amount negated, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  40. final def notify(): Unit

    Definition Classes
    AnyRef
  41. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  42. def plus(amountToAdd: Long, unit: TemporalUnit): Duration

    Returns a copy of this duration with the specified duration added.

    Returns a copy of this duration with the specified duration added.

    The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by this method. The unit must either have an TemporalUnit#isDurationEstimated() exact duration or be ChronoUnit#DAYS which is treated as 24 hours. Other units throw an exception.

    This instance is immutable and unaffected by this method call.

    amountToAdd

    the amount of the period, measured in terms of the unit, positive or negative

    unit

    the unit that the period is measured in, must have an exact duration, not null

    returns

    a { @code Duration} based on this duration with the specified duration added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  43. def plus(duration: Duration): Duration

    Returns a copy of this duration with the specified duration added.

    Returns a copy of this duration with the specified duration added.

    This instance is immutable and unaffected by this method call.

    duration

    the duration to add, positive or negative, not null

    returns

    a { @code Duration} based on this duration with the specified duration added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  44. def plusDays(daysToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in 24 hour days added.

    Returns a copy of this duration with the specified duration in 24 hour days added.

    This instance is immutable and unaffected by this method call.

    daysToAdd

    the days to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified days added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  45. def plusHours(hoursToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in hours added.

    Returns a copy of this duration with the specified duration in hours added.

    This instance is immutable and unaffected by this method call.

    hoursToAdd

    the hours to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified hours added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  46. def plusMillis(millisToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in milliseconds added.

    Returns a copy of this duration with the specified duration in milliseconds added.

    This instance is immutable and unaffected by this method call.

    millisToAdd

    the milliseconds to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified milliseconds added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  47. def plusMinutes(minutesToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in minutes added.

    Returns a copy of this duration with the specified duration in minutes added.

    This instance is immutable and unaffected by this method call.

    minutesToAdd

    the minutes to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified minutes added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  48. def plusNanos(nanosToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in nanoseconds added.

    Returns a copy of this duration with the specified duration in nanoseconds added.

    This instance is immutable and unaffected by this method call.

    nanosToAdd

    the nanoseconds to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified nanoseconds added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  49. def plusSeconds(secondsToAdd: Long): Duration

    Returns a copy of this duration with the specified duration in seconds added.

    Returns a copy of this duration with the specified duration in seconds added.

    This instance is immutable and unaffected by this method call.

    secondsToAdd

    the seconds to add, positive or negative

    returns

    a { @code Duration} based on this duration with the specified seconds added, not null

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  50. def subtractFrom(temporal: Temporal): Temporal

    Subtracts this duration from the specified temporal object.

    Subtracts this duration from the specified temporal object.

    This returns a temporal object of the same observable type as the input with this duration subtracted.

    In most cases, it is clearer to reverse the calling pattern by using Temporal#minus(TemporalAmount).

    // these two lines are equivalent, but the second approach is recommended
    dateTime = thisDuration.subtractFrom(dateTime);
    dateTime = dateTime.minus(thisDuration);
    

    The calculation will subtract the seconds, then nanos. Only non-zero amounts will be added.

    This instance is immutable and unaffected by this method call.

    temporal

    the temporal object to adjust, not null

    returns

    an object of the same type with the adjustment made, not null

    Definition Classes
    DurationTemporalAmount
    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

    DateTimeException

    if unable to subtract

  51. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  52. def toDays: Long

    Gets the number of days in this duration.

    Gets the number of days in this duration.

    This returns the total number of days in the duration by dividing the number of seconds by 86400. This is based on the standard definition of a day as 24 hours.

    This instance is immutable and unaffected by this method call.

    returns

    the number of days in the duration, may be negative

  53. def toHours: Long

    Gets the number of hours in this duration.

    Gets the number of hours in this duration.

    This returns the total number of hours in the duration by dividing the number of seconds by 3600.

    This instance is immutable and unaffected by this method call.

    returns

    the number of hours in the duration, may be negative

  54. def toMillis: Long

    Converts this duration to the total length in milliseconds.

    Converts this duration to the total length in milliseconds.

    If this duration is too large to fit in a long milliseconds, then an exception is thrown.

    If this duration has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.

    returns

    the total length of the duration in milliseconds

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  55. def toMinutes: Long

    Gets the number of minutes in this duration.

    Gets the number of minutes in this duration.

    This returns the total number of minutes in the duration by dividing the number of seconds by 60.

    This instance is immutable and unaffected by this method call.

    returns

    the number of minutes in the duration, may be negative

  56. def toNanos: Long

    Converts this duration to the total length in nanoseconds expressed as a long.

    Converts this duration to the total length in nanoseconds expressed as a long.

    If this duration is too large to fit in a long nanoseconds, then an exception is thrown.

    returns

    the total length of the duration in nanoseconds

    Exceptions thrown
    ArithmeticException

    if numeric overflow occurs

  57. def toString(): String

    A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.

    A string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.

    The format of the returned string will be PTnHnMnS, where n is the relevant hours, minutes or seconds part of the duration. Any fractional seconds are placed after a decimal point i the seconds section. If a section has a zero value, it is omitted. The hours, minutes and seconds will all have the same sign.

    Examples:

    "20.345 seconds"                 -> "PT20.345S
    "15 minutes" (15 * 60 seconds)   -> "PT15M"
    "10 hours" (10 * 3600 seconds)   -> "PT10H"
    "2 days" (2 * 86400 seconds)     -> "PT48H"
    
    Note that multiples of 24 hours are not output as days to avoid confusion with Period.

    returns

    an ISO-8601 representation of this duration, not null

    Definition Classes
    Duration → AnyRef → Any
  58. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  59. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  60. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  61. def withNanos(nanoOfSecond: Int): Duration

    Returns a copy of this duration with the specified nano-of-second.

    Returns a copy of this duration with the specified nano-of-second.

    This returns a duration with the specified nano-of-second, retaining the seconds part of this duration.

    This instance is immutable and unaffected by this method call.

    nanoOfSecond

    the nano-of-second to represent, from 0 to 999,999,999

    returns

    a { @code Duration} based on this period with the requested nano-of-second, not null

    Exceptions thrown
    DateTimeException

    if the nano-of-second is invalid

  62. def withSeconds(seconds: Long): Duration

    Returns a copy of this duration with the specified amount of seconds.

    Returns a copy of this duration with the specified amount of seconds.

    This returns a duration with the specified seconds, retaining the nano-of-second part of this duration.

    This instance is immutable and unaffected by this method call.

    seconds

    the seconds to represent, may be negative

    returns

    a { @code Duration} based on this period with the requested seconds, not null

Inherited from Serializable

Inherited from Ordered[Duration]

Inherited from Comparable[Duration]

Inherited from TemporalAmount

Inherited from AnyRef

Inherited from Any

Ungrouped