Check if two date periods overlap [duplicate]

You can use Joda-Time for this.

It provides the class Interval which specifies a start and end instants and can check for overlaps with overlaps(Interval).

Something like

DateTime now = DateTime.now();

DateTime start1 = now;
DateTime end1 = now.plusMinutes(1);

DateTime start2 = now.plusSeconds(50);
DateTime end2 = now.plusMinutes(2);

Interval interval = new Interval( start1, end1 );
Interval interval2 = new Interval( start2, end2 );

System.out.println( interval.overlaps( interval2 ) );

prints

true

since the end of the first interval falls between the start and end of the second interval.

Leave a Comment