JavaEssential standard classesDate and time

Comparing dates and time

The classes LocalDate, LocalTime, LocalDateTime have methods for comparing their instances. The methods compare instances in the chronological order (in the order of time).


The method compareTo

The method compareTo compares this instance and another one passed as the method's argument. It returns 0 if they are equal, a negative value if this instance is less than other, and a positive value if this instance is greater than other.

Here is an example of comparing two instances of the class LocalDate:

LocalDate date1 = LocalDate.parse("2017-01-02");
LocalDate date2 = LocalDate.parse("2017-12-12");

date1.compareTo(date1); // 0, date1 and date1 are equal
date1.compareTo(date2); // -11, date1 is less than date2
date2.compareTo(date1); // 11, date2 is greater than date1

The class LocalTime has the same method:

LocalTime time1 = LocalTime.parse("15:30:10");
LocalTime time2 = LocalTime.parse("17:50:30");

time1.compareTo(time1); // 0, time1 and time1 are equal
time1.compareTo(time2); // -1, time1 is less than time2
time2.compareTo(time1); // 1, time2 is greater than time1

as well as LocalDateTime:

LocalDateTime dateTime1 = LocalDateTime.parse("2017-01-01T20:30"); // 1 January 2017, 20:30
LocalDateTime dateTime2 = LocalDateTime.parse("2017-01-02T23:00"); // 2 January 2017, 23:00

dateTime1.compareTo(dateTime1); // 0, dateTime1 and dateTime are equal
dateTime1.compareTo(dateTime2); // -1, dateTime1 is less than dateTime2
dateTime2.compareTo(dateTime1); // 1, dateTime2 is greater than dateTime1

The methods isAfter, isBefore and isEqual

The classes have some concise methods to compare dates and time with a clean boolean result.

  • The method isAfter returns true, only if this instance is strictly after another instance passed as the argument, otherwise, the method returns false.
  • The method isBefore returns true, only if this instance is strictly before an instance passed as the argument, otherwise, the method returns false.
  • The method isEqual returns true, if both instances are equal, otherwise, the method returns false.

Here is an example of comparing two instances of the LocalDate class.
LocalDate d1 = LocalDate.of(2017, 11, 30);
LocalDate d2 = LocalDate.of(2017, 12, 1);

d1.isEqual(d1); // true
d1.isEqual(d2); // false

d1.isBefore(d2); // true
d1.isBefore(d1); // false
d2.isBefore(d1); // false

d2.isAfter(d1); // true
d2.isAfter(d2); // false
d1.isAfter(d2); // false

Here is an example of comparing two instances of the LocalTime class.

LocalTime time1 = LocalTime.of(14, 20); // 14:20
LocalTime time2 = LocalTime.of(15, 55); // 15:55
LocalTime time3 = LocalTime.of(16, 40); // 16:40
        
time1.isBefore(time2); // true
time3.isBefore(time2); // false
time3.isBefore(time3); // false

time2.isAfter(time1);  // true
time2.isAfter(time3);  // false

And here is an example of comparing two instances of the LocalDateTime class.

LocalDateTime dateTime1 = LocalDateTime.parse("2017-12-01T21:30"); // 1 December 2017, 21:30
LocalDateTime dateTime2 = LocalDateTime.parse("2017-12-02T21:30"); // 2 December 2017, 21:30

dateTime1.isEqual(dateTime2); // false
dateTime2.isEqual(dateTime2); // true

dateTime1.isBefore(dateTime2); // true
dateTime1.isAfter(dateTime2); // false
dateTime2.isAfter(dateTime1); // true

So, it's possible to compare standard classes representing dates and time in almost the same unified way. But keep in mind, the class LocalTime doesn't have the method isEqual. You can use the method equals instead.
How did you like the theory?
Report a typo