Scala - How to compare two dates
In this scala example we will cover how to compare two dates programatically. To compare two dates we will use compareTo() method. This compareTo() method return 3 types of values which are
- 0 if the both given dates are equal
- >0 if the current date comes after the given date
- <0 if the current date comes before the given date
Here two compare two dates first we will create two date object by passing the given dates
Scala example to compare two dates using compareTo() method
import java.util.Date; object Sample { var result: Int = 0; result = date1.compareTo(date2); if (result == 0) result = date1.compareTo(date3); if (result == 0) |
The above example will compare given dates and return below output
date1 is comes before date2
date1 and date3 are equal
|