Validate Date with Java Regular Expression example

In this tutorial we are going to see how to validate date format with Java Regular Expressions.The basic policy about  date of the form “dd/mm/yyyy” is that:

  • It should start with two digits from 01 – 31 or from 1 – 31.
  • It must be followed by ‘/’.
  • It should be followed by two digits from 01- 12 or from 1 – 12.
  • It must then be followed by “/”
  • It must the be followed by a number between 1900 and 2099.

So this is the regular expression we are going to use for 12 hours format validation:


You can take a look at the Pattern class documentation to learn how to construct your own regular expressions according to your policy.

The hard thing in date formats is that it is a bit hard to embed other constraints in tha regulare expression, like even months have 31 days and odd have 30, February has 28 or 29 years on the year leap.

1. Validator clas.

This is the class that we are going to use for date format validation.


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateFormatValidator{

  private Pattern pattern;
  private Matcher matcher;

  private static final String DATE_VALIDATION_PATTERN = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";

  public DateFormatValidator(){
	  pattern = Pattern.compile(DATE_VALIDATION_PATTERN);

   public boolean validate(final String date){

     matcher = pattern.matcher(date);




         String dd =;
	     String mm =;
	     int yy = Integer.parseInt(;

	     if (dd.equals("31") &&  (mm.equals("4") || mm .equals("6") || mm.equals("9") ||
                  mm.equals("11") || mm.equals("04") || mm .equals("06") ||
                  mm.equals("09"))) {
			return false;
	     } else if (mm.equals("2") || mm.equals("02")) {

		  if(yy % 4==0){
			  if(dd.equals("30") || dd.equals("31")){
				  return false;
				  return true;
				  return false;
				  return true;
		return true;				 
    	      return false;
	  return false;

2. Unit Testing our DateFormatValidator class

For unit testing we are going to use JUnit. Unit testing is very important in these situations because they provide good feedback about the correctness of our regular expressions. You can test your program and reassure that your regular expression meets the rules on your policy about the form of date format.

This is a basic test class:


import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

public class DateFormatValidatorTest {

	private String arg;
	private static DateFormatValidator dateFormatValidator;
	private Boolean expectedValidation;

	public DateFormatValidatorTest(String str, Boolean expectedValidation) {
		this.arg = str;
		this.expectedValidation = expectedValidation;

	public static void initialize() {
		dateFormatValidator = new DateFormatValidator();

	public static Collection<Object[]> data() {
		Object[][] data = new Object[][] {
				{ "11/11/11", false },    // we want the full year description
				{ "2013/23/12" , false }, // wrong format
				{ "1-10-2013", false },   // wrong format
				{ "32/12/2020", false },  // out of range
				{ "31/04/2013", false },  // April has 30 days


				{ "5/1/2013", true },                         
				{ "04/04/2009", true },
				{ "28/2/2012", true } };

		return Arrays.asList(data);

	public void test() {
		Boolean res = dateFormatValidator.validate(this.arg);
		String validv = (res) ? "valid" : "invalid";
		System.out.println("Date Format "+arg+ " is " + validv);
		assertEquals("Result", this.expectedValidation, res);


Date Format 11/11/11 is invalid
Date Format 2013/23/12 is invalid
Date Format 1-10-2013 is invalid
Date Format 32/12/2020 is invalid
Date Format 31/04/2013 is invalid
Date Format 5/1/2013 is valid
Date Format 04/04/2009 is valid
Date Format 28/2/2012 is valid

This was an exampl on how to validate date format with Java Regular Expression.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

pattern = Pattern.compile(“((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])”);
matcher = pattern.matcher(date);



int yy = Integer.parseInt(;
String mm =;
String dd =;

its Not working please update the

Back to top button