Java Check if Date in between Start and End Dates

Tags: Java Date DateUtil

In this Java tutorial, we learn how to validate if a given Date value is in between start and end Date values in Java programming language.

How to validate a Date is in between two Date values in Java

At this first step, create a new Java class named DateUtil.

In this new class, implement a static method named validateDateBetween(Date valueToValidate, Date start, Date end), the method implementation to check whether the first argument Date value is in between start and end Date values or not as the following Java code.

DateUtil.java

import java.util.Date;

public class DateUtil {

    /**
     * This method to check if a Date value in between given start and end Date values.
     * @param valueToValidate the Date value to validate.
     * @param start the start Date object.
     * @param end the end Date object.
     * @return true if the Date in between start and end otherwise return false.
     */
    public static boolean validateDateBetween(Date valueToValidate, Date start, Date end) {
        if (valueToValidate == null || start == null || end == null) {
            throw new IllegalArgumentException("Invalid argument.");
        }

        if (start.compareTo(end) > 0) {
            throw new IllegalArgumentException("The end date should greater than start date.");
        }

        return valueToValidate.compareTo(start) >= 0
                && valueToValidate.compareTo(end) <= 0;
    }
}

In the following example Java code, we learn how to use the DateUtil.validateDateBetween() in Java program to check if a given Date value is in between provided start and end Date values or not.

ValidateDateBetweenExample.java

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ValidateDateBetweenExample {
    public static void main(String... args) {
        Date date1 = new GregorianCalendar(2022, Calendar.AUGUST, 10).getTime();
        Date date2 = new GregorianCalendar(2022, Calendar.AUGUST, 20).getTime();

        Date start = new GregorianCalendar(2022, Calendar.AUGUST, 5).getTime();
        Date end = new GregorianCalendar(2022, Calendar.AUGUST, 15).getTime();

        // Check if Date in between Start and End Dates
        boolean result1 = DateUtil.validateDateBetween(date1, start, end);
        boolean result2 = DateUtil.validateDateBetween(date2, start, end);

        System.out.println("start: " + start);
        System.out.println("end: " + end);
        System.out.println("\ndate1: " + date1);
        System.out.println("date1 is between start and end: " + result1);
        System.out.println("\ndate2: " + date2);
        System.out.println("date2 is between start and end: " + result2);
    }
}
The output as below.
start: Fri Aug 05 00:00:00 ICT 2022
end: Mon Aug 15 00:00:00 ICT 2022

date1: Wed Aug 10 00:00:00 ICT 2022
date1 is between start and end: true

date2: Sat Aug 20 00:00:00 ICT 2022
date2 is between start and end: false

Happy Coding 😊

Java Check if Calendar is Week Day or Weekend Day

Java Check if Date is Week Day or Weekend Day