HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Regular Expressions / Java date validation using RegEx

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 !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Amit Kushawaha

    July 30, 2019

    Does not validate the leap year like 30/02/2019 in this case it should return false but returns true.

    • Lokesh Gupta

      July 31, 2019

      Hi Amit, It is just validating the date format against a predefined pattern. To check if it is actual date in calendar, parse it to LocalDate object in strict mode.

      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
      
      try 
      {
        	LocalDate.parse("2016-02-30", 
                  formatter.withResolverStyle(ResolverStyle.STRICT));
      } 
      catch (DateTimeException ex) 
      {
        	System.out.println(ex.getMessage());
      }
  2. mofis khan

    June 6, 2016

    how to validate the date to present date

  3. Priyanka

    March 30, 2016

    If i want to check time as well as and along with date wat will be the regex pattern??

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces