compare To
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.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
import kotlin.time.Duration.Companion.hours
fun main() {
//sampleStart
// Finding out which of two instants is earlier
fun randomInstant() = Instant.fromEpochMilliseconds(
Random.nextLong(Instant.DISTANT_PAST.toEpochMilliseconds(), Instant.DISTANT_FUTURE.toEpochMilliseconds())
)
repeat(100) {
val instant1 = randomInstant()
val instant2 = randomInstant()
// in the UTC time zone, earlier instants are represented as earlier datetimes
check((instant1 < instant2) ==
(instant1.toLocalDateTime(TimeZone.UTC) < instant2.toLocalDateTime(TimeZone.UTC)))
}
//sampleEnd
}