Kotlin:
日付を比較する

How to: (方法)

Kotlin uses the java.time.LocalDate class for date comparisons. Here’s how:

import java.time.LocalDate

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

    println(date1.isBefore(date2))  // Check if date1 is before date2
    println(date1.isAfter(date2))   // Check if date1 is after date2
    println(date1.isEqual(date2))   // Check if both dates are equal
}

Sample output if run on April 2, 2023:

true
false
false

Deep Dive (掘り下げ)

Kotlin relies on java.time (introduced in Java 8) for date operations. Before Java 8, java.util.Date and java.util.Calendar were common but flawed due to design and thread-safety issues. Alternatives include Joda-Time, but java.time is now the recommended approach.

Comparing dates is crucial for sorting, scheduling tasks, or validating time periods. Kotlin enhances Java’s approach with extension functions and better null-safety.

See Also (関連情報)

  • Official Kotlin documentation on Java interoperability: Kotlin and Java Interop
  • Oracle’s Java documentation on the java.time package: java.time
  • GitHub repository for the ThreeTen-Backport project, a backport of java.time for Java 6 & 7: ThreeTen-Backport