Duration

value class Duration : Comparable<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

Companion
Link copied to clipboard
object Companion

Functions

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

isFinite
Link copied to clipboard
fun isFinite(): Boolean

Returns true, if the duration value is finite.

isInfinite
Link copied to clipboard
fun isInfinite(): Boolean

Returns true, if the duration value is infinite.

isNegative
Link copied to clipboard
fun isNegative(): Boolean

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

isPositive
Link copied to clipboard
fun isPositive(): Boolean

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

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

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

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

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

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

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

toDouble
Link copied to clipboard
fun toDouble(unit: DurationUnit): Double

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

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

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

toIsoString
Link copied to clipboard
fun toIsoString(): String

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

toLong
Link copied to clipboard
fun toLong(unit: DurationUnit): Long

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

toLongMilliseconds
Link copied to clipboard
fun toLongMilliseconds(): Long

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

toLongNanoseconds
Link copied to clipboard
fun toLongNanoseconds(): Long

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

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

unaryMinus
Link copied to clipboard
operator fun unaryMinus(): Duration

Returns the negative of this value.

Properties

absoluteValue
Link copied to clipboard
val absoluteValue: Duration

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

inDays
Link copied to clipboard
val inDays: Double

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

inHours
Link copied to clipboard
val inHours: Double

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

inMicroseconds
Link copied to clipboard
val inMicroseconds: Double

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

inMilliseconds
Link copied to clipboard
val inMilliseconds: Double

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

inMinutes
Link copied to clipboard
val inMinutes: Double

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

inNanoseconds
Link copied to clipboard
val inNanoseconds: Double

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

inSeconds
Link copied to clipboard
val inSeconds: Double

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

inWholeDays
Link copied to clipboard
val inWholeDays: Long

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

inWholeHours
Link copied to clipboard
val inWholeHours: Long

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

inWholeMicroseconds
Link copied to clipboard
val inWholeMicroseconds: Long

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

inWholeMilliseconds
Link copied to clipboard
val inWholeMilliseconds: Long

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

inWholeMinutes
Link copied to clipboard
val inWholeMinutes: Long

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

inWholeNanoseconds
Link copied to clipboard
val inWholeNanoseconds: Long

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

inWholeSeconds
Link copied to clipboard
val inWholeSeconds: Long

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

Extensions

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

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