• Home
  • Kotlin compare two dates android

Kotlin compare two dates android

To compare two dates in Kotlin, you can use the compareTo() function of the Date class.

Here’s an example of how to compare two dates in Kotlin:

val date1 = Date()
val date2 = Date()

if (date1.compareTo(date2) > 0) {
// date1 is after date2
} else if (date1.compareTo(date2) < 0) {
// date1 is before date2
} else {
// date1 is the same as date2
}

Alternatively, you can use the before(), after(), and equals() functions of the Date class to compare two dates:

 

val date1 = Date()
val date2 = Date()

if (date1.before(date2)) {
// date1 is before date2
} else if (date1.after(date2)) {
// date1 is after date2
} else {
// date1 is the same as date2
}
val date1 = Date()
val date2 = Date()

if (date1.equals(date2)) {
// date1 is the same as date2
} else {
// date1 is not the same as date2
}