to Epoch Milliseconds
Начиная с Kotlin: 2.3
fun toEpochMilliseconds(): Long
Возвращает количество миллисекунд от момента эпохи 1970-01-01T00:00:00Z.
Дробная часть миллисекунды округляется к нулю до целого количества миллисекунд.
Если результат не помещается в Long, возвращает Long.MAX_VALUE для положительного результата или Long.MIN_VALUE для отрицательного результата.
См. также
Примеры
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
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
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-instant/to-epoch-milliseconds.html