toEpochMilliseconds
Returns the number of milliseconds from the epoch instant 1970-01-01T00:00:00Z
.
Any fractional part of a millisecond is rounded toward zero to the whole number of milliseconds.
If the result does not fit in Long, returns Long.MAX_VALUE for a positive result or Long.MIN_VALUE for a negative result.
Since Kotlin
2.1See also
Samples
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds
fun main() {
//sampleStart
val currentInstant = Clock.System.now()
// The number of whole milliseconds that have passed since the Unix epoch.
println(currentInstant.toEpochMilliseconds())
// When an Instant is created with fromEpochMilliseconds,
// toEpochMilliseconds will return the exact value provided.
val instant1 = Instant.fromEpochMilliseconds(0)
println(instant1.toEpochMilliseconds()) // 0
val instant2 = Instant.fromEpochMilliseconds(1_000_000_000_123)
println(instant2.toEpochMilliseconds()) // 1000000000123
// Any fractional part of a millisecond is rounded toward zero to the whole number of milliseconds.
val instant3 = Instant.fromEpochSeconds(1_000_000_000, nanosecondAdjustment = 123_999_999)
println(instant3.toEpochMilliseconds()) // 1000000000123
//sampleEnd
}