- I have two date ranges, (start1,end1):::>>date1 && (start2,end2):::>>date2 .
I want to check if the two dates isOverLaped.
My flow chart I assume "<>=" operators is valid for comparing.
boolean isOverLaped(Date start1,Date end1,Date start2,Date end2) { if (start1>=end2 && end2>=start2 && start2>=end2) { return false; } else { return true; } }
- Any Suggestion will be appreciated.

- 5,632
- 3
- 27
- 47

- 7,831
- 6
- 45
- 73
-
2Java does not support overloading operators. – Sotirios Delimanolis Sep 21 '13 at 22:07
-
This is the correct answer: http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap – BlueLettuce16 Aug 07 '14 at 07:25
4 Answers
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.

- 303,325
- 100
- 852
- 1,154

- 274,122
- 60
- 696
- 724
-
Thanks for you workaround but we just wanna use that operators. and assume unsorted dates bounds. – Khaled Lela Sep 21 '13 at 22:16
-
1@KhaledLela The `Interval` class throws an exception if the argument aren't in the correct order. You can use that to your advantage to instantiate correctly (or use the milliseconds value of the date decide which comes first). The JodaTime solution gives for much cleaner code than using `>` and `<`. – Sotirios Delimanolis Sep 21 '13 at 22:28
-
To use this example code with existing java.util.Date objects, simply pass each Date instance to a Joda-Time `DateTime` constructor: `DateTime dateTimeStart1 = new DateTime( start1 );` – Basil Bourque Jan 17 '14 at 05:43
-
boolean overlap(Date start1, Date end1, Date start2, Date end2){
return start1.getTime() <= end2.getTime() && start2.getTime() <= end1.getTime();
}

- 77,785
- 15
- 98
- 110
-
1Except `start1` is declared as type `Date` and so are the others. – Sotirios Delimanolis Sep 21 '13 at 22:09
-
-
What about the dates period is not sorted, like "end1 may be come before start1". – Khaled Lela Sep 21 '13 at 22:12
-
1So are you saying your date ranges might not even be valid? If you need to validate THAT as well, I would write a separate method. – Dawood ibn Kareem Sep 21 '13 at 22:12
//the inserted interval date is start with fromDate1 and end with toDate1
//the date you want to compare with start with fromDate2 and end with toDate2
if ((int)(toDate1 - fromDate2).TotalDays < 0 )
{ return true;}
else
{
Response.Write("<script>alert('there is an intersection between the inserted date interval and the one you want to compare with')</script>");
return false;
}
if ((int)(fromDate1 - toDate2).TotalDays > 0 )
{ return true;}
else
{
Response.Write("<script>alert('there is an intersection between the inserted date interval and the one you want to compare with')</script>");
return false;
}

- 971
- 1
- 7
- 5
-
1Is your code written in some language other than Java? The question asked for Java. In Java we cannot use the MINUS SIGN to subtract a pair of Date instances. And I do not recognize the `.TotalDays`. – Basil Bourque Jan 17 '14 at 05:48
-
You have two intervals, i1 and i2. There are six cases for how the intervals can be temporally related (at least in a Newtonian world view) but only two are important: if i1 is entirely before i2 or i1 is entirely after i2; otherwise the two intervals are overlapping (the other four cases are i1 contains i2, i2 contains i1, i1 contains the start of i2 and i1 contains the end of i2). Assume i1 and i2 are of type Interval that have Date fields beginTime and endTime. The function then is (note, the assumption here is that if i1 starts at the same time i2 ends, or vice versa, we don't consider that an overlap and we assme for a given interval endTime.before(beginTime) is false):
boolean isOverlapped(Interval i1, Interval i2) {
return i1.endTime.before(i2.beginTime) || i1.beginTime.after(i2.endTime);
}
In the original question, you specify DateTime instead of Date. In java, Date has both date and time. This is in contrast to sql where Date does not have a time element while DateTime does. That is a point of confusion that I stumbled across when I first started using sql after having done only java for many years. Anyway, I hope this explanation is helpful.

- 324
- 1
- 3
- 14
-
This is incorrect. If interval `i1` is entirely before interval `i2`, this will still return true. – Dawood ibn Kareem Jun 05 '17 at 19:25
-
This is really wrong. The correct comparing is: `!(beginPeriod1.isAfter(endPeriod2) || beginPeriod2.isAfter(endPeriod1))` – Artur Rashitov Nov 08 '18 at 08:02