toString
Converts this instant to the ISO 8601 string representation, for example, 2023-01-02T23:40:57.120Z
.
The representation uses the UTC-SLS time scale instead of UTC. In practice, this means that leap second handling will not be readjusted to the UTC. Leap seconds will not be added or skipped, so it is impossible to acquire a string where the component for seconds is 60, and for any day, it's possible to observe 23:59:59.
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
// The string representation of the current instant in ISO 8601 format.
val currentInstant = Clock.System.now()
println(currentInstant.toString())
// The string representation of the Unix epoch.
val unixEpochInstant = Instant.fromEpochSeconds(0)
println(unixEpochInstant.toString()) // 1970-01-01T00:00:00Z
// The string representation of an instant.
val instant = Instant.fromEpochMilliseconds(1_000_000_000_123)
println(instant.toString()) // 2001-09-09T01:46:40.123Z
//sampleEnd
}