Порівняння двох дат

Kotlin:
Порівняння двох дат

How to (Як це зробити):

import java.time.LocalDate

fun main() {
    val date1 = LocalDate.of(2023, 4, 1)
    val date2 = LocalDate.now()

    println(date1.isBefore(date2)) // true if date1 is before date2
    println(date1.isAfter(date2))  // true if date1 is after date2
    println(date1.isEqual(date2))  // true if date1 is equal to date2
}

Output varies depending on today’s date.

Deep Dive (Поглиблений Аналіз):

Kotlin, like Java, has rich support for date-time operations using java.time package. Historically, Java’s old Date and Calendar classes were troublesome. Kotlin, benefiting from Java’s evolution, recommends java.time (JSR-310) for modern date-time handling.

Alternatives? You could use Date and Calendar from old Java times, but that’s masochism. Stick to java.time.

Implementation details? LocalDate is just for dates, no times. If you need to compare time, you’d use LocalDateTime or Instant. ZonedDateTime is there for handling time zones.

See Also (Дивіться також):