
Patrick B. answered 09/07/19
Math and computer tutor/teacher
For OOP, the DateTime variable (presuming it is an object) can be compared outright; It's class perhaps overloads the basic comparison operators < , >, and ==
Dates like these are typically considered long integers which contain the number of milliseconds or system ticks from a starting point called the EPOCH.
anyways, given this functionality, the overlap can be tested by comparing
(start1 < start2) AND (stop1 < start2)
It should also be guaranteed that the start1 < start2....
Here's a java routine that does this algorithm:
class OverlapIntervals
{
public static void main(String args[])
{
long start1, start2, stop1, stop2;
//UNCOMMENT the one you want to test... then recompile and run
start1=10; stop1=20; start2=15; stop2 = 30; // they overlap
//start1=10; stop1=15; start2=20; stop2 = 25; // they do not overlap
//start1 = 15; stop1 = 30; start2 = 10; stop2 = 20; // they overlap
//start1 = 20; stop1 = 25; start2=10; stop2=15; // they do not overlap
//start1 = 25; stop1=30; start2= 15; stop2= 40; // they overlap; in fact one is subset of the other
// start1 = 5; stop1 = 7; start2 = 25; stop2=28; they do not overlap
//start1 = 10; stop1 = 15; start2=15; stop2=20; //they overlap at exactly one point
//IF NECESSARY, swaps the pairs so that start1 < start2
if (start2 < start1)
{
long startTemp = start1;
long stopTemp = stop1;
start1 = start2;
stop1 = stop2;
start2 = startTemp;
stop2 = stopTemp;
}
// Is it considered OVERLAP if stop1 == start2 ???? see last test case above
// if NOT, then you must change the last condition to stop1 <= start2
if ((start1 < start2) && ( stop1 < start2))
{
System.out.println(" They do not overlap ");
//sets the return value here to reflect they do not overlap
}
else
{
System.out.println(" they overlap ");
//sets the return value here to reflect they overlap
}
//return
} //main
} //class