How to check if Date is within a certain range in Java?

A Java example to check if a provided date is within a range of 3 momths before and after current date. The idea is quite simple, just use Calendar class to roll the month back and forward to create a “date range”, and use the Date.before() and Date.after() to check if the Date is within the range.


if (dateToValidate.before(currentDateAfter3Months.getTime())
		&& dateToValidate.after(currentDateBefore3Months.getTime())) {

See full example.


package com.mkyong.date;

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

public class DateValidator {

	public boolean isThisDateWithin3MonthsRange(String dateToValidate,
			String dateFromat) {

		SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
		sdf.setLenient(false);
		try {

			// if not valid, it will throw ParseException
			Date date = sdf.parse(dateToValidate);

			// current date after 3 months
			Calendar currentDateAfter3Months = Calendar.getInstance();
			currentDateAfter3Months.add(Calendar.MONTH, 3);

			// current date before 3 months
			Calendar currentDateBefore3Months = Calendar.getInstance();
			currentDateBefore3Months.add(Calendar.MONTH, -3);

			/*************** verbose ***********************/
			System.out.println("\n\ncurrentDate : "
					+ Calendar.getInstance().getTime());
			System.out.println("currentDateAfter3Months : "
					+ currentDateAfter3Months.getTime());
			System.out.println("currentDateBefore3Months : "
					+ currentDateBefore3Months.getTime());
			System.out.println("dateToValidate : " + dateToValidate);
			/************************************************/
			
			if (date.before(currentDateAfter3Months.getTime())
					&& date.after(currentDateBefore3Months.getTime())) {

				//ok everything is fine, date in range
				return true;

			} else {

				return false;

			}

		} catch (ParseException e) {

			e.printStackTrace();
			return false;
		}

	}

}

A simple unit test to prove above code is working correctly, if you run below unit test, all he cases will be passed.


package com.mkyong.test;

import org.junit.*;
import com.mkyong.date.DateValidator;
import static org.junit.Assert.*;

public class DateValidatorRangeTest {

	private DateValidator dateValidator;

	@Before
	public void init() {
		dateValidator = new DateValidator();
	}

	@Test
	public void testDateWithinRange_1() {
		assertTrue(dateValidator.isThisDateWithin3MonthsRange("31/01/2012", "dd/MM/yyyy"));
	}

	@Test
	public void testDateWithinRange_2() {
		assertFalse(dateValidator.isThisDateWithin3MonthsRange("31/01/2011", "dd/MM/yyyy"));
	}
	
	@Test
	public void testDateWithinRange_3() {
		assertTrue(dateValidator.isThisDateWithin3MonthsRange("20/02/2012", "dd/MM/yyyy"));
	}
	
	@Test
	public void testDateWithinRange_4() {
		assertFalse(dateValidator.isThisDateWithin3MonthsRange("21/05/2012", "dd/MM/yyyy"));
	}
	
}

All cases are passed and output following extra info on console.


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 31/01/2012


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 31/01/2011


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 20/02/2012


currentDate : Mon Feb 20 13:36:59 SGT 2012
currentDateAfter3Months : Sun May 20 13:36:59 SGT 2012
currentDateBefore3Months : Sun Nov 20 13:36:59 SGT 2011
dateToValidate : 21/05/2012

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
MaxDineshSrizzFarhad Taraporeakki Recent comment authors
newest oldest most voted
Max
Guest
Max

Can you help me to write a condition that validates +/- 3 days to a given any date. Format is like: “2019-11-04”

So if the Nov 8 is given then its fail if it is Nov 7, 6, 5 and 3, 2, 1 is a Pass since it is within the range.

Your help would be greatly appreciated

Dinesh
Guest
Dinesh

Awesome, This was really helpful for me.. Thanks.

Farhad Tarapore
Guest
Farhad Tarapore

I have a string like “09:00” which I use a SimpleDateFormat(“HH:mm”) to parse. But the DATE portion of this TIME, gets set as Jan 1 , 1970. How do I get the Date object returned by the parse method to have today’s date, but the time as formatted by the string given?

akki
Guest
akki

how to validate date of birth if i click on it it should not be empty could u tell me how its possible

dave
Guest
dave

What format are you parsing your date into? “MM/dd/yyyy”?

Srizz
Guest
Srizz

I have a doubt. I was to retrieve dates from a duration that repeats every monday. for example, if i have duration from 5th Feb 2015 to 28th Feb 2015, i want to knw the dates that fall under this duration that repeats every monday, wednesday and thursday. The recurrence of days is stored in database as sun, mon, tue, wed, thu, fri, sat