How do you determine if a date is between two other dates with Java 8's java.time API?
Asked
Active
Viewed 4,649 times
-6
Basil Bourque
- 262,936
- 84
- 758
- 1,028
Matthias M
- 135
- 3
- 18
-
What research have you done with the API on the JavaDoc site? – AlBlue Jul 06 '15 at 16:36
1 Answers
8
The LocalDate class has
isAfter(LocalDate other)
isBefore(LocalDate other)
isEqual(LocalDate other)
methods for comparisons with other dates. Here is an example:
LocalDate today = LocalDate.now();
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDate yesterday = LocalDate.now().minusDays(1);
if(today.isAfter(yesterday) && today.isBefore(tomorrow))
System.out.println("Today is... today!");
AlBlue
- 22,017
- 14
- 65
- 90
Luigi Cortese
- 10,221
- 5
- 35
- 48