Java date validation using RegEx

In this Java date validation using regex, we will learn to validate simple date formats such as mm/dd/yy, mm/dd/yyyy, dd/mm/yy and dd/mm/yyyy. Here we want to use a regex that simply checks whether the input is a valid date without trying to eliminate things such as February 31st.

We might think that something as conceptually trivial as a date validation should be an easy job for a regular expression. But it isn’t. The main issue is that regular expressions don’t deal directly with numbers.

We can’t tell a regular expression to “match a number between 1 and 31”. Rather regular expressions work character by character.

We use '3[01]|[12][0-9]|0?[1-9]' to match 3 followed by 0 or 1, or to match 1 or 2 followed by any digit, or to match an optional 0 followed by 1 to 9. Because of this, you have to choose how simple or how accurate you want your regular expression to be.

1. Java date validation regex – allow leading zeros to be omitted

Regex : ^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$

List dates = new ArrayList();
dates.add("1/1/11");
dates.add("01/01/11");
dates.add("01/01/2011");
dates.add("01/1/2011");
dates.add("1/11/2011");
dates.add("1/11/11");
dates.add("11/1/11");

String regex = "^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$";

Pattern pattern = Pattern.compile(regex);

for(String date : dates)
{
	Matcher matcher = pattern.matcher(date);
	System.out.println(date +" : "+ matcher.matches());
}

Program output.

1/1/11 : 		true
01/01/11 : 		true
01/01/2011 : 	true
01/1/2011 : 	true
1/11/2011 : 	true
1/11/11 : 		true
11/1/11 : 		true

2. Java date validation regex – require leading zeros

Regex : ^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$

List dates = new ArrayList();

//With leading zeros
dates.add("01/01/11");
dates.add("01/01/2011");

//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");
dates.add("1/11/2011");
dates.add("1/11/11");
dates.add("11/1/11");

String regex = "^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$";

Pattern pattern = Pattern.compile(regex);

for(String date : dates)
{
	Matcher matcher = pattern.matcher(date);
	System.out.println(date +" : "+ matcher.matches());
}

Program output.

01/01/11 : 		true
01/01/2011 : 	true

1/1/11 : 		false
01/1/2011 : 	false
1/11/2011 : 	false
1/11/11 : 		false
11/1/11 : 		false

3. Java date validation regex – match “mm/dd/yyyy” with required leading zeros

Regex : ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$

Java program to validate date format mm/dd/yyyy.

List dates = new ArrayList();

//With leading zeros
dates.add("01/01/11");
dates.add("01/01/2011");

//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");

String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";

Pattern pattern = Pattern.compile(regex);

for(String date : dates)
{
	Matcher matcher = pattern.matcher(date);
	System.out.println(date +" : "+ matcher.matches());
}

Program output.

01/01/11 : 		false
01/01/2011 : 	true
1/1/11 : 		false
01/1/2011 : 	false

4. Java date validation regex – match “dd/mm/yyyy” with required leading zeros

The regular expression for validating date format dd/mm/yyyy.

Regex : ^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$

List dates = new ArrayList();
//With leading zeros
dates.add("07/13/2011");
dates.add("13/07/2011");
//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");

String regex = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";

Pattern pattern = Pattern.compile(regex);

for(String date : dates)
{
	Matcher matcher = pattern.matcher(date);
	System.out.println(date +" : "+ matcher.matches());
}

Program output.

07/13/2011 : 	false
13/07/2011 : 	true
1/1/11 : 		false
01/1/2011 : 	false

Feel free to use and edit above regular expressions for date validation to suit your needs.

Happy Learning !!

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode