Check if two Date Ranges Overlap or not in Java

In this tutorial, we are going to learn to check if two Date Ranges Overlap or not in Java.

There would be 4 possible overlaps. Consider two date ranges, start date be s1, the end date is e1 for the first range and s2 be the start date and e2 be the end date for the second range. we need to consider the following cases.

The figure below shows different cases of overlapping:

Different cases of overlapping

Case 1: As shown in the above link, s2 comes in between s1 and e1.

Case 2: For case 2, e2 comes in between s1 and e1.

Case 3: s2 and e2 come in between s1 and e1.

case 4: s1 and e1 come in between s2 and s2.

Our goal is to check if two Date Ranges Overlap or not in Java. For that, we need to follow the following steps:

  • Import some packages as we are handling with dates.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
  • Create an object of SimpliDateFormat.
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
  • Create the object of Date and initialize Date values in the above format.
Date s1=s.parse("2000-01-01");
Date e1=s.parse("2001-01-01");
Date s2=s.parse("2000-01-02");
Date e2=s.parse("2001-01-02");
  • Now for case 1 as s2 comes between s1 and e1, we can write the condition as:
s1.before(s2) && e1.after(s2)

Note: s1.before(s2) can be read as, s1 comes before s2, whereas e1.after(s2) can be read as, e1 comes after s2.

  • Similarly, for case 2, e2 comes between s1 and e1:
s1.before(e2) && e1.after(e2)
  • For case 3, s2 and e2 come between s1 and e1:
s1.before(s2) && e1.after(e2)
  • Similarly, for case 4, s1 and e1 come between s2 and e2:
s1.after(s2) && e1.before(e2)

Implementation:

Code for our task:

Example 1:

//import packages
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class date {

  public static void main(String[] args) throws ParseException {
    //create object of SimpleDateFormat
    SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
    //create object of Date and initialize values
    Date s1=s.parse("2000-01-01");
    Date e1=s.parse("2001-01-01");
    Date s2=s.parse("2000-01-02");
    Date e2=s.parse("2001-01-02");
    
    System.out.print("Start date "+s.format(s1));
    System.out.println("  End date "+s.format(e1));
    
    System.out.print("Start date "+s.format(s2));
    System.out.println("  End date "+s.format(e2));
    //conditions for different cases
    if(s1.before(s2) && e1.after(s2) ||
       s1.before(e2) && e1.after(e2) ||
       s1.before(s2) && e1.after(e2) ||
       s1.after(s2) && e1.before(e2) )
    {
      System.out.print("They overlap");
    }
  
    else {
      System.out.print("They don't overlap");
    }
    
  }

}

Output 1:

Start date 2000-01-01 End date 2001-01-01
Start date 2000-01-02 End date 2001-01-02
They overlap

Output 2:

Start date 2000-01-01 End date 2001-01-01
Start date 2002-01-01 End date 2003-01-01
They don't overlap

Example 2:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class date2 {

  public static void main(String[] args) throws ParseException {
    SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
    Date s1=s.parse("2000-01-01");
    Date e1=s.parse("2001-01-01");
    Date s2=s.parse("2000-01-02");
    Date e2=s.parse("2001-01-02");
    
    System.out.print("Start date "+s.format(s1));
    System.out.println("  End date "+s.format(e1));
    
    System.out.print("Start date "+s.format(s2));
    System.out.println("  End date "+s.format(e2));

    if(s1.compareTo(s2)<0 && e1.compareTo(s2)>0 ||
       s1.compareTo(e2)<0 && e1.compareTo(e2)>0 ||
       s1.compareTo(s2)<0 && e1.compareTo(e2)>0 ||
       s1.compareTo(s2)>0 && e1.compareTo(e2)<0 )
      
    {
      System.out.print("They overlap");
    }
    else {
      System.out.println("They don't overlap");
    }
  }

}

Note: Here, s1.compareTo(s2)<0 can be read as s1 comes before s2 and e1.compareTo(s2)>0 can be read as e1 comes after s2.

Output 1:

Start date 2000-01-01 End date 2001-01-01
Start date 2000-01-02 End date 2001-01-02
They overlap

Output 2:

Start date 2000-01-01 End date 2001-01-01
Start date 2002-01-01 End date 2003-01-01
They don't overlap

This is how we can check if two Date Ranges Overlap or not in Java. I hope you find this tutorial useful.

Also, read:

Leave a Reply

Your email address will not be published. Required fields are marked *