fromEpochMilliseconds

fun fromEpochMilliseconds(epochMilliseconds: Long): Instant(source)

Returns an Instant that is epochMilliseconds number of milliseconds from the epoch instant 1970-01-01T00:00:00Z.

Every value of epochMilliseconds is guaranteed to be representable as an Instant.

Note that Instant also supports nanosecond precision via fromEpochSeconds.

Since Kotlin

2.1

See also

Samples

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds

fun main() { 
   //sampleStart 
   // Creating an Instant representing the Unix epoch.
val instant1 = Instant.fromEpochMilliseconds(0)
// Creating an Instant from a specific number of milliseconds since the Unix epoch.
val instant2 = Instant.fromEpochMilliseconds(1_000_000_000_123)

// toEpochMilliseconds returns the exact millisecond value provided during construction.
println(instant1.toEpochMilliseconds()) // 0
println(instant2.toEpochMilliseconds()) // 1000000000123

// The string representation of the constructed instants.
println(instant1) // 1970-01-01T00:00:00Z
println(instant2) // 2001-09-09T01:46:40.123Z 
   //sampleEnd
}