compareTo

open operator override fun compareTo(other: Instant): Int(source)

Compares this instant with the other instant.

Returns zero if this instant represents the same moment as the other (meaning they are equal to one another), a negative number if this instant is earlier than the other, and a positive number if this instant is later than the other.

Since Kotlin

2.1

Samples

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

fun main() { 
   //sampleStart 
   val instant1 = Instant.parse("1970-01-01T00:00:00Z") // The Unix epoch.
val instant2 = Instant.parse("2025-01-06T13:36:44Z")
val instant3 = Instant.fromEpochSeconds(0) // The Unix epoch.

println("instant1 < instant2 is ${instant1 < instant2}") // true
println("instant3 > instant1 is ${instant3 > instant1}") // false
println("instant3 == instant1 is ${instant3 == instant1}") // true 
   //sampleEnd
}