-6

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Matthias M
  • 135
  • 3
  • 19

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
  • 23,254
  • 14
  • 71
  • 91
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48