Instant

A moment in time.

A point in time must be uniquely identified in a way that is independent of a time zone. For example, 1970-01-01, 00:00:00 does not represent a moment in time since this would happen at different times in different time zones: someone in Tokyo would think it is already 1970-01-01 several hours earlier than someone in Berlin would. To represent such entities, use the LocalDateTime from kotlinx-datetime. In contrast, "the moment the clocks in London first showed 00:00 on Jan 1, 2000" is a specific moment in time, as is "1970-01-01, 00:00:00 UTC+0", so it can be represented as an Instant.

Instant uses the UTC-SLS (smeared leap second) time scale. This time scale doesn't contain instants corresponding to leap seconds, but instead "smears" positive and negative leap seconds among the last 1000 seconds of the day when a leap second happens.

Obtaining the current moment

The Clock interface is the primary way to obtain the current moment:

val clock: Clock = Clock.System
val instant = clock.now()

The Clock.System implementation uses the platform-specific system clock to obtain the current moment. Note that this clock is not guaranteed to be monotonic, and the user or the system may adjust it at any time, so it should not be used for measuring time intervals. For that, consider using TimeSource.Monotonic and TimeMark instead of Clock.System and Instant.

Arithmetic operations

The plus and minus operators can be used to add Durations to and subtract them from an Instant:

Clock.System.now() + 5.seconds // 5 seconds from now

Also, there is a minus operator that returns the Duration representing the difference between two instants:

val kotlinRelease = Instant.parse("2016-02-15T02:00T12:00:00+03:00")
val kotlinStableDuration = Clock.System.now() - kotlinRelease

Platform specifics

On the JVM, there are Instant.toJavaInstant() and java.time.Instant.toKotlinInstant() extension functions to convert between kotlin.time and java.time objects used for the same purpose. Likewise, on JS, there are Instant.toJSDate() and Date.toKotlinInstant() extension functions.

For technical reasons, converting Instant to and from Foundation's NSDate is provided in kotlinx-datetime via Instant.toNSDate() and NSDate.toKotlinInstant() extension functions. These functions might become available in kotlin.time in the future.

Construction, serialization, and deserialization

fromEpochSeconds can be used to construct an instant from the number of seconds since 1970-01-01T00:00:00Z (the Unix epoch). epochSeconds and nanosecondsOfSecond can be used to obtain the number of seconds and nanoseconds since the epoch.

val instant = Instant.fromEpochSeconds(1709898983, 123456789)
instant.epochSeconds // 1709898983
instant.nanosecondsOfSecond // 123456789

fromEpochMilliseconds allows constructing an instant from the number of milliseconds since the epoch. toEpochMilliseconds can be used to obtain the number of milliseconds since the epoch. Note that Instant supports nanosecond precision, so converting to milliseconds is a lossy operation.

val instant1 = Instant.fromEpochSeconds(1709898983, 123456789)
instant1.nanosecondsOfSecond // 123456789
val milliseconds = instant1.toEpochMilliseconds() // 1709898983123
val instant2 = Instant.fromEpochMilliseconds(milliseconds)
instant2.nanosecondsOfSecond // 123000000

parse and toString methods can be used to obtain an Instant from and convert it to a string in the (ISO 8601 extended format)https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations, which includes a time zone designator.

val instant = Instant.parse("2023-01-02T22:35:01+01:00")
instant.toString() // 2023-01-02T21:35:01Z

Since Kotlin

2.1

Types

Link copied to clipboard
object Companion
Since Kotlin 2.1

Properties

Link copied to clipboard

The number of seconds from the epoch instant 1970-01-01T00:00:00Z rounded down to a Long number.

Since Kotlin 2.1
Link copied to clipboard

Returns true if the instant is Instant.DISTANT_FUTURE or later.

Since Kotlin 2.1
Link copied to clipboard

Returns true if the instant is Instant.DISTANT_PAST or earlier.

Since Kotlin 2.1
Link copied to clipboard

The number of nanoseconds by which this instant is later than epochSeconds from the epoch instant.

Since Kotlin 2.1

Functions

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

Compares this instant with the other instant.

Since Kotlin 2.1
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
open operator override fun equals(other: Any?): Boolean
Since Kotlin 2.1
Link copied to clipboard
open override fun hashCode(): Int
Since Kotlin 2.1
Link copied to clipboard
operator fun minus(duration: Duration): Instant

Returns an instant that is the result of subtracting the specified duration from this instant.

Since Kotlin 2.1
operator fun minus(other: Instant): Duration

Returns the Duration between two instants: other and this.

Since Kotlin 2.1
Link copied to clipboard
operator fun plus(duration: Duration): Instant

Returns an instant that is the result of adding the specified duration to this instant.

Since Kotlin 2.1
Link copied to clipboard

Returns the number of milliseconds from the epoch instant 1970-01-01T00:00:00Z.

Since Kotlin 2.1
Link copied to clipboard

Converts this kotlin.time.Instant value to a java.time.Instant value.

Since Kotlin 2.1
Link copied to clipboard

Converts the Instant to an instance of JS Date.

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

Converts this instant to the ISO 8601 string representation, for example, 2023-01-02T23:40:57.120Z.

Since Kotlin 2.1