lundi 24 juin 2019

How to check if one date period overlapping another date period

So I have this function I created called isDateOverlapping. It takes in (of all type LocalDate):

  • Start Date 1
  • End Date 1
  • Start Date 2
  • End Date 2

What this function does is it tells me if the 2 date periods are overlapping or not.

For example if I have 1 period from 2019-06-15 to 2019-06-18 and another period from 2019-06-15 to 2019-06-12. It would return true because the 2 date periods overlap.

However my function doesn't seem to always return the right answer. Could you please help.

public boolean isDateOverlapping(LocalDate start1, LocalDate end1, LocalDate start2, LocalDate end2) {

    if (start1.isAfter(start2) && start2.isBefore(start1)) {
        return true;

    } else if (end1.isBefore(end2) && start1.isAfter(start2)) {
        return true;
    } else if (start1.isAfter(end1) && end2.isBefore(start2)) {
        return true;
    }

    return false;

}

Aucun commentaire:

Enregistrer un commentaire