-6

How do you determine if a date is between two other dates with Java 8's java.time API?

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
Matthias M
  • 135
  • 3
  • 18

1 Answers1

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