How to find common dates between two date ranges in Java

In this tutorial, we will learn how to find common dates between two date ranges in Java. To do this, we will be using the LocalDate Java class. This will give us an immutable date-time object with which we can represent a date.

Common dates between two date ranges in Java

Here are the steps you can follow to get the common dates:

  1. Getting the two ranges of dates
    Firstly, we need to get the two ranges of dates. The dates will be stored as an object of the LocalDate class. We will also use the DateTimeFormatter class to parse the input with the right format. The format we are using here is DD/MM/YYYY.

    LocalDate date;
    Scanner sc = new Scanner(System.in);
    String userInput;
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d/M/yyyy");
    //Getting user input of date ranges in the format DD/MM/YYYY
    System.out.println("Enter the start date of the first range: ");
    System.out.println("Date(mm/dd/yyyy):");
    userInput = sc.next();
    date = LocalDate.parse(userInput, dateFormat);
  2. Creating a list of dates in the given range
    Next, we will create two lists of dates that store all the dates in the given range. To do this, we will use the interface LongStream to help us create the list. We also need to count the number of days in the range before creating the list. This can be done using the until() method from the LocalDate class as shown below.

    List<LocalDate> range1;
    //Number of days in the first range is stored in days1
    long days1 = date1.until(date2, ChronoUnit.DAYS);
    //making a list of dates in from the first range of dates
    range1 = LongStream.rangeClosed(0,days1).mapToObj(date1::plusDays).collect(Collectors.toList());
    
  3. Getting the common dates
    We can find the common dates between the two lists using the method called retainAll(). This will retain only the elements that are in common between the two lists and store it in the list that called this method.

    //Find the common dates
    range1.retainAll(range2);
    //printing the number of common dates
    System.out.println("Number of days in common: "+range1.size());
  4. Printing the dates
    We can print the dates from the list in the following way.

    for(int i=0; i<range1.size(); i++) {
      //Printing the common dates in the format DD/MM/YYYY
      System.out.println(range1.get(i).getDayOfMonth()+"/"+range1.get(i).getMonthValue()+"/"+range1.get(i).getYear());
    }

     

Sample Code

Here is a sample code that follows the above-mentioned steps to get the common dates between the ranges.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class GetCommonDates {
  LocalDate date1, date2, date3, date4;
  List<LocalDate> range1;
  List<LocalDate> range2;

  //Method to get the range of dates
  void getrange() {
    Scanner sc = new Scanner(System.in);
    String userInput;
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d/M/yyyy");
    //Getting user input of date ranges in the format DD/MM/YYYY
    System.out.println("Enter the start date of the first range: ");
    System.out.println("Date(mm/dd/yyyy):");
    userInput = sc.next();
    date1 = LocalDate.parse(userInput, dateFormat);

    System.out.println("Enter the end date of the first range: ");
    System.out.println("Date(mm/dd/yyyy):");
    userInput = sc.next();
    date2 = LocalDate.parse(userInput, dateFormat);

    System.out.println("Enter the start date of the second range: ");
    System.out.println("Date(mm/dd/yyyy):");
    userInput = sc.next();
    date3 = LocalDate.parse(userInput, dateFormat);

    System.out.println("Enter the end date of the second range: ");
    System.out.println("Date(mm/dd/yyyy):");
    userInput = sc.next();
    date4 = LocalDate.parse(userInput, dateFormat);

  }
  
  //Method to find the common dates between two date ranges
  void common() {
    Scanner sc = new Scanner(System.in);
    String c;
    //Number of days in the first range is stored in days1
    long days1 = date1.until(date2, ChronoUnit.DAYS);
    //making a list of dates in from the first range of dates
    range1 = LongStream.rangeClosed(0,days1).mapToObj(date1::plusDays).collect(Collectors.toList());
    
    //number of days in the second range is stored in days2
    long days2 = date3.until(date4, ChronoUnit.DAYS);
    //making a list of dates from the second second range of dates
    range2 = LongStream.rangeClosed(0,days2).mapToObj(date3::plusDays).collect(Collectors.toList());
    
    //Find the common dates
    range1.retainAll(range2);
    //printing the number of common dates
    System.out.println("Number of days in common: "+range1.size());
    
    if(range1.size()==0) {
      return;
    }
    
    //Print the common dates
    System.out.println("Do you want to print the common dates?(Y/N):");
    c = sc.next();
    if(c.equals("Y")||c.equals("y")) {
      printDates();
    }
    else if(!(c.equals("N")||c.equals("n"))) {
      System.out.println("Invalid choice.");
    }
  }
  
  void printDates() {
    System.out.println("The common dates are:");
    for(int i=0; i<range1.size(); i++) {
      //Printing the common dates in the format DD/MM/YYYY
      System.out.println(range1.get(i).getDayOfMonth()+"/"+range1.get(i).getMonthValue()+"/"+range1.get(i).getYear());
    }
  }
  
  public static void main(String args[]) {
    GetCommonDates CDates = new GetCommonDates();
    CDates.getrange();
    CDates.common();
  }
}

 

Output

Enter the start date of the first range: 
Date(mm/dd/yyyy):
1/9/2021
Enter the end date of the first range: 
Date(mm/dd/yyyy):
30/9/2021
Enter the start date of the second range: 
Date(mm/dd/yyyy):
16/9/2021
Enter the end date of the second range: 
Date(mm/dd/yyyy):
7/10/2021
Number of days in common: 15
Do you want to print the common dates?(Y/N):
y
The common dates are:
16/9/2021
17/9/2021
18/9/2021
19/9/2021
20/9/2021
21/9/2021
22/9/2021
23/9/2021
24/9/2021
25/9/2021
26/9/2021
27/9/2021
28/9/2021
29/9/2021
30/9/2021

Enter the start date of the first range: 
Date(mm/dd/yyyy):
2/8/2021
Enter the end date of the first range: 
Date(mm/dd/yyyy):
19/8/2021
Enter the start date of the second range: 
Date(mm/dd/yyyy):
13/9/2021
Enter the end date of the second range: 
Date(mm/dd/yyyy):
18/10/2021
Number of days in common: 0

Here is a tutorial that you can check out next: How to extract dates from a .txt file in Java.

Leave a Reply

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