Duration

Represents the amount of time one instant of time is away from another instant.

A negative duration is possible in a situation when the second instant is earlier than the first one.

The type can store duration values up to ±146 years with nanosecond precision, and up to ±146 million years with millisecond precision. If a duration-returning operation provided in kotlin.time produces a duration value that doesn't fit into the above range, the returned Duration is infinite.

An infinite duration value Duration.INFINITE can be used to represent infinite timeouts.

To construct a duration use either the extension function toDuration, or the extension properties hours, minutes, seconds, and so on, available on Int, Long, and Double numeric types.

To get the value of this duration expressed in a particular duration units use the functions toInt, toLong, and toDouble or the properties inWholeHours, inWholeMinutes, inWholeSeconds, inWholeNanoseconds, and so on.

Since Kotlin

1.6

Types

Link copied to clipboard
object Companion
Since Kotlin 1.6

Properties

Link copied to clipboard

Returns the absolute value of this value. The returned value is always non-negative.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of days.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of hours.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of microseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of milliseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of minutes.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of nanoseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Double number of seconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of days.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of hours.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of microseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of milliseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of minutes.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of nanoseconds.

Since Kotlin 1.6
Link copied to clipboard

The value of this duration expressed as a Long number of seconds.

Since Kotlin 1.6

Functions

Link copied to clipboard
open operator override fun compareTo(other: Duration): Int

Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

Since Kotlin 1.6
Link copied to clipboard
infix inline fun <T> Comparable<T>.compareTo(other: T): Int

Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

Since Kotlin 1.6
Link copied to clipboard
operator fun div(scale: Double): Duration
operator fun div(scale: Int): Duration

Returns a duration whose value is this duration value divided by the given scale number.

Since Kotlin 1.6
operator fun div(other: Duration): Double

Returns a number that is the ratio of this and other duration values.

Since Kotlin 1.6
Link copied to clipboard

Returns true, if the duration value is finite.

Since Kotlin 1.6
Link copied to clipboard

Returns true, if the duration value is infinite.

Since Kotlin 1.6
Link copied to clipboard

Returns true, if the duration value is less than zero.

Since Kotlin 1.6
Link copied to clipboard

Returns true, if the duration value is greater than zero.

Since Kotlin 1.6
Link copied to clipboard
operator fun minus(other: Duration): Duration

Returns a duration whose value is the difference between this and other duration values.

Since Kotlin 1.6
Link copied to clipboard
operator fun plus(other: Duration): Duration

Returns a duration whose value is the sum of this and other duration values.

Since Kotlin 1.6
Link copied to clipboard
operator fun times(scale: Double): Duration
operator fun times(scale: Int): Duration

Returns a duration whose value is this duration value multiplied by the given scale number.

Since Kotlin 1.6
Link copied to clipboard
inline fun <T> toComponents(action: (seconds: Long, nanoseconds: Int) -> T): T

Splits this duration into seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.

Since Kotlin 1.6
inline fun <T> toComponents(action: (minutes: Long, seconds: Int, nanoseconds: Int) -> T): T

Splits this duration into minutes, seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.

Since Kotlin 1.6
inline fun <T> toComponents(action: (hours: Long, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T

Splits this duration into hours, minutes, seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.

Since Kotlin 1.6
inline fun <T> toComponents(action: (days: Long, hours: Int, minutes: Int, seconds: Int, nanoseconds: Int) -> T): T

Splits this duration into days, hours, minutes, seconds, and nanoseconds and executes the given action with these components. The result of action is returned as the result of this function.

Since Kotlin 1.6
Link copied to clipboard

Returns the value of this duration expressed as a Double number of the specified unit.

Since Kotlin 1.6
Link copied to clipboard
fun toInt(unit: DurationUnit): Int

Returns the value of this duration expressed as an Int number of the specified unit.

Since Kotlin 1.6
Link copied to clipboard

Returns an ISO-8601 based string representation of this duration.

Since Kotlin 1.6
Link copied to clipboard

Converts kotlin.time.Duration value to java.time.Duration value.

Since Kotlin 1.6
Link copied to clipboard

Returns the value of this duration expressed as a Long number of the specified unit.

Since Kotlin 1.6
Link copied to clipboard

Returns the value of this duration expressed as a Long number of milliseconds.

Since Kotlin 1.6
Link copied to clipboard

Returns the value of this duration expressed as a Long number of nanoseconds.

Since Kotlin 1.6
Link copied to clipboard
open override fun toString(): String

Returns a string representation of this duration value expressed as a combination of numeric components, each in its own unit.

Since Kotlin 1.6
fun toString(unit: DurationUnit, decimals: Int = 0): String

Returns a string representation of this duration value expressed in the given unit and formatted with the specified decimals number of digits after decimal point.

Since Kotlin 1.6
Link copied to clipboard
operator fun unaryMinus(): Duration

Returns the negative of this value.

Since Kotlin 1.6