How to check if date is valid in Java

In Java, you can use SimpleDateFormat class to check if a provided date is valid.

1. DateValidator example


package com.mkyong.date;

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

public class DateValidator {

	public boolean isThisDateValid(String dateToValidate, String dateFromat){
		
		if(dateToValidate == null){
			return false;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
		sdf.setLenient(false);
		
		try {
			
			//if not valid, it will throw ParseException
			Date date = sdf.parse(dateToValidate);
			System.out.println(date);
		
		} catch (ParseException e) {
			
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
}

By default, SimpleDateFormat.setLenient() is set to true, you should always turn it off to make your date validation more strictly.

2. Unit Test

All unit test cases are passed.


package com.mkyong.test;

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

public class DateValidatorTest {

	private DateValidator dateValidator;

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

	@Test
	public void testDateIsNull() {
		assertFalse(dateValidator.isThisDateValid(null, "dd/MM/yyyy"));
	}

	@Test
	public void testDayIsInvalid() {
		assertFalse(dateValidator.isThisDateValid("32/02/2012", "dd/MM/yyyy"));
	}

	@Test
	public void testMonthIsInvalid() {
		assertFalse(dateValidator.isThisDateValid("31/20/2012", "dd/MM/yyyy"));
	}

	@Test
	public void testYearIsInvalid() {
		assertFalse(dateValidator.isThisDateValid("31/20/19991", "dd/MM/yyyy"));
	}

	@Test
	public void testDateFormatIsInvalid() {
		assertFalse(dateValidator.isThisDateValid("2012/02/20", "dd/MM/yyyy"));
	}

	@Test
	public void testDateFeb29_2012() {
		assertTrue(dateValidator.isThisDateValid("29/02/2012", "dd/MM/yyyy"));
	}

	@Test
	public void testDateFeb29_2011() {
		assertFalse(dateValidator.isThisDateValid("29/02/2011", "dd/MM/yyyy"));
	}

	@Test
	public void testDateFeb28() {
		assertTrue(dateValidator.isThisDateValid("28/02/2011", "dd/MM/yyyy"));
	}

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

	@Test
	public void testDateIsValid_2() {
		assertTrue(dateValidator.isThisDateValid("30/04/2012", "dd/MM/yyyy"));
	}

	@Test
	public void testDateIsValid_3() {
		assertTrue(dateValidator.isThisDateValid("31/05/2012", "dd/MM/yyyy"));
	}

}

References

  1. Date validation in Java (regex example)
  2. SimpleDateFormat JavaDoc

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
17 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
19 Comment authors
DucazzSaurabh KulkarniMohit GuptatendoteChansopheak Khun Recent comment authors
newest oldest most voted
adithya kanduri
Guest
adithya kanduri

hey mykong..but 12012-04-05 will get failed (for yyyy-MM-dd format).Any idea how to avoid this

Ken
Guest
Ken

You have a false assertion in testYearIsInvalid() as your month value is already wrong.
assertFalse(dateValidator.isThisDateValid(“31/20/19991”, “dd/MM/yyyy”));

SimpleDateFormat#parse allows years like 999999 even if they are not 4 characters.

Mohit Gupta
Guest
Mohit Gupta

i am also facing same problem. Event if you keep character after four digit of year it will parse it. Does not understand why?

Ducazz
Guest
Ducazz

Typo: dateFromat

Saurabh Kulkarni
Guest
Saurabh Kulkarni

Even after setting sdf.setLenient(false); if dateToValidate = “2018-08-01T00:00:00Z” & DATE_FORMAT = “yyyy-MM-dd”, Date date = sdf.parse(dateToValidate); works fine which should fail.

Chansopheak Khun
Guest
Chansopheak Khun

test with 29/02/2017 not work.
test with 29/02/2016 work.

tendote
Guest
tendote

Because 2016 is leap year so that year has 29 days of February

Ankit
Guest
Ankit

assertTrue(dateValidator.isThisDateValid(“31/05/12”, “dd/MM/yyyy”));
With SimpleDateFormat above scenario passes the test but it should not as year format is not correct.

Ganesh
Guest
Ganesh

One of my colleague we should the code which decides the return value on the exception. What’s your say on that??

Bhupendra
Guest
Bhupendra

try public void testDateIsValid_3() {
assertTrue(dateValidator.isThisDateValid(“31/05/20ik”, “dd/MM/yyyy”));
}

doug
Guest
doug

it would help to put actual imports instead of your own modified ones for the noobs like me

Gowtham
Guest
Gowtham

Isn’t there any other alternative to it because this seems to be an awkward practice (don’t take my wrong) because in any situation, we need to try our level best to prevent an exception to occur.

Thanks.

penchal
Guest
penchal

How to check date format only not date from user with the mm/dd/yyyy.

aj
Guest
aj

it will fail this:

@Test
public void testDayIsInvalid() {
assertFalse(isThisDateValid(“01/01/02/2012”, “dd/MM/yyyy”));
}

but You can add checking against regexp to avoid this situation, or try add :

return sdf.format(sdf.parse(dateToValidate)).equals(dateToValidate);

to isThisDateValid method
to

Sander
Guest
Sander

setLenient(false) !!!!!!!!

thanks! that was just what I needed :)

Tarun Trehan
Guest
Tarun Trehan

Hi,

Nice post. Leveraged this info to validate against multiple and varying date formats. Listed solution here http://allzhere.in/2013/07/07/java-date-validation-against-multiple-formats/

Juan R.
Guest
Juan R.

Thank you so much!, just what I was looking for, simple as that!.

Cheers!

Zak
Guest
Zak

This is damn so simple solution… thanks a lot.

Andreas Schwarz
Guest
Andreas Schwarz

test your java knowlege – http://java-quizapp.rhcloud.com