Compare two dates in Kotlin
This article explores different ways to compare two dates in Kotlin.
1. Using compareTo() function
The standard solution to compare two Date objects is by using the compareTo() function. It returns a value
= 0, if both dates are equal.< 0, if date is before the specified date.> 0, if date is after the specified date.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.text.SimpleDateFormat import java.util.* fun main() { val d1 = "12/24/2018" val d2 = "10/10/2017" val sdf = SimpleDateFormat("MM/dd/yyyy") val firstDate: Date = sdf.parse(d1) val secondDate: Date = sdf.parse(d2) val cmp = firstDate.compareTo(secondDate) when { cmp > 0 -> { System.out.printf("%s is after %s", d1, d2) } cmp < 0 -> { System.out.printf("%s is before %s", d1, d2) } else -> { print("Both dates are equal") } } } |
Output:
12/24/2018 is after 10/10/2017
You can also use the compareTo() function for comparing LocalDate, LocalTime, and LocalDateTime objects.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.time.LocalDate import java.time.format.DateTimeFormatter fun main() { val d1 = "12/24/2018" val d2 = "10/10/2017" val formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy") val firstDate: LocalDate = LocalDate.parse(d1, formatter) val secondDate: LocalDate = LocalDate.parse(d2, formatter) when { firstDate > secondDate -> { System.out.printf("%s is after %s", d1, d2) } firstDate < secondDate -> { System.out.printf("%s is before %s", d1, d2) } else -> { print("Both dates are equal") } } } |
Output:
12/24/2018 is after 10/10/2017
2. Using before(), after(), and equals() function
Alternatively, you can also use the before(), after() and equals() from Date class, which returns a boolean value based on the comparison result.
after()returns true if the date is after the specified date.before()returns true if the date is before the specified date.equals()returns true if both dates are equal.
The following example demonstrates the usage of these methods to compare two LocalDate:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.text.SimpleDateFormat import java.util.* fun main() { val d1 = "12/24/2018" val d2 = "10/10/2017" val sdf = SimpleDateFormat("MM/dd/yyyy") val firstDate: Date = sdf.parse(d1) val secondDate: Date = sdf.parse(d2) when { firstDate.after(secondDate) -> { System.out.printf("%s is after %s", d1, d2) } firstDate.before(secondDate) -> { System.out.printf("%s is before %s", d1, d2) } firstDate == secondDate -> { print("Both dates are equal") } } } |
Output:
12/24/2018 is after 10/10/2017
3. Using isBefore(), isAfter(), isEqual() functions
To compare LocalDate, LocalTime, or LocalDateTime objects, you can use the isBefore(), isAfter(), isEqual() functions. The following example demonstrates the usage of these methods to compare two LocalDate:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.time.LocalDate import java.time.format.DateTimeFormatter fun main() { val d1 = "12/24/2018" val d2 = "10/10/2017" val formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy") val firstDate: LocalDate = LocalDate.parse(d1, formatter) val secondDate: LocalDate = LocalDate.parse(d2, formatter) when { firstDate.isAfter(secondDate) -> { System.out.printf("%s is after %s", d1, d2) } firstDate.isBefore(secondDate) -> { System.out.printf("%s is before %s", d1, d2) } firstDate.isEqual(secondDate) -> { print("Both dates are equal") } } } |
Output:
12/24/2018 is after 10/10/2017
That’s all about comparing two dates in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)