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. An infinite duration value Duration.INFINITE can be used to represent infinite timeouts.

The type can store duration values up to ±146 years with nanosecond precision, and up to ±146 million years with millisecond precision.

To construct a duration, use either the extension function toDuration available on Int, Long, and Double numeric types, or the Duration companion object functions Duration.hours, Duration.minutes, Duration.seconds, and so on, taking Int, Long, or Double numbers as parameters.

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.3

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open operator override fun compareTo(other: Duration): Int
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.

operator fun div(other: Duration): Double

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

Link copied to clipboard

Returns true, if the duration value is finite.

Link copied to clipboard

Returns true, if the duration value is infinite.

Link copied to clipboard

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

Link copied to clipboard

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

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

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

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

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

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.

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.

inline fun <T> toComponents(action: (minutes: Int, 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.

inline fun <T> toComponents(action: (hours: Int, 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.

inline fun <T> toComponents(action: (days: Int, 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.

Link copied to clipboard

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

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

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

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

Returns a string representation of this duration value expressed in the unit which yields the most compact and readable number value.

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.

Link copied to clipboard
operator fun unaryMinus(): Duration

Returns the negative of this value.

Properties

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Extensions

Link copied to clipboard
@ExperimentalTime
inline fun Duration.toJavaDuration(): Duration

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