'How to check if time period is overlapping another time period in java using LocaDateTime?

I need to check if time period overlaps another time, why I always get false if even I know that this times won't overlaps.

public static void main(String[] args) {

    LocalDateTime dateToAdd = LocalDateTime.parse("2022-02-10T12:30");

    LocalDateTime date1 = LocalDateTime.parse("2022-02-10T10:00");
    LocalDateTime date2 = LocalDateTime.parse("2022-02-10T11:00");
    LocalDateTime date3 = LocalDateTime.parse("2022-02-10T14:00");
    LocalDateTime date4 = LocalDateTime.parse("2022-02-10T17:00");
    LocalDateTime date5 = LocalDateTime.parse("2022-02-10T19:00");

    List<LocalDateTime> dates = List.of(date1, date2, date3, date4, date5);

    boolean b = dates.stream()
            .allMatch(dateTime -> isOverlapping(dateTime, dateTime.plusHours(1), dateToAdd, dateToAdd.plusHours(1)));

    System.out.println(b);

}

public static boolean isOverlapping(LocalDateTime start1, LocalDateTime end1, LocalDateTime start2, LocalDateTime end2) {
    return start1.isBefore(end2) && start2.isBefore(end1);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source