How to validate date using Java regular expression

My earlier post on how to validate email address, SSN and phone number validation using Java regex still attracts a lot of visitors. Today I realized that another piece of data that many programmers need to validate is the date. Many Java applications have to process input date values, so I thought it will be beneficial to this blog readers to show how regular expression can be used to validate date in Java.

First I’ll show you how to validate date using java regex in US format and later I’ll show you how that same logic can be applied to validate date in English format (used in most countries outside North America).

Let’s begin by writing the Java code to validate the date.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExDateValidation{
	public static boolean  isValidDate(String date){
		boolean isValid = false;
		String expression = "^[0-1][1-2][- / ]?(0[1-9]|[12][0-9]|3[01])[- /]?(18|19|20|21)\\d{2}$";
		/*
		* ^[0-1][1-2] : The month starts with a 0 and a digit between 1 and 2
		* [- / ]?: Followed by  an optional "-" or "/".
		* (0[1-9]|[12][0-9]|3[01]) : The day part must be either between 01-09, or 10-29 or 30-31.
		* [- / ]?: Day part will be followed by  an optional "-" or "/".
		* (18|19|20|21)\\d{2}$ : Year begins with either 18, 19, 20 or 21 and ends with two digits.
		*/
		CharSequence inputStr = date;
		Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(inputStr);
		if(matcher.matches()){
			isValid = true;
		}
		return isValid;
	}

	/*Here is a simple test method to test an input date using Java regex*/

	public static void main(String args[]){
		boolean isValid = RegExDateValidation.isValidDate(args[0]);
		System.out.println(args[0] + " is " + (isValid?"valid":"invalid"));
	}
}

 

I included the explanation on how the regular expression is constructed in the code, however the day part of the date needs a little more explanation.
The expression for day validation is a group of three mutually exclusive sub-expressions or alternatives. Only one of these conditions can be true. (0[1-9]|[12][0-9]|3[01]). This is done to verify that the combination of the digits in the day string falls between 01 and 31.

  • The first part checks that the day has a value between 01 and 09. Therefore the expression starts
    with a 0 followed by a range of digit from 1 to 9.
  • The second part validates the days between 10 to 29. [12] means that the first digit can be a 1 or a 2. [0-9] shows that the second digit can be any value from 0 to 9.
  • The third subgroup checks for days 30 and 31.

This expression will validate dates in US format from years 1800 to 2199. For example 03/15/2009, 10/12/1885 are valid dates but the date 31/01/1999 will not pass validation. For date in European (English) format, we can rearrange the regular expression as follows.

^(0[1-9]|[12][0-9]|3[01])[- /]?[0-1][1-9][- / ]?(18|19|20|21)\\d{2,4}

Here the day comes before the month. So the date 17/08/2009 is valid but 08/17/2009 will not pass this validation. The logic is same but the elements are re-arranged. You can use following method to switch between American and English date formats dynamically at runtime based on an additional boolean parameter.

public static boolean checkDate(String date, boolean isEnglish){
		String monthExpression = "[0-1][1-9]";
		String dayExpression = "(0[1-9]|[12][0-9]|3[01])";
		boolean isValid = false;
		//RegEx to validate date in US format.
		String expression = "^" + monthExpression +"[- / ]?" + dayExpression + "[- /]?(18|19|20|21)\\d{2}";
		if(isEnglish){
			//RegEx to validate date in Metric format.
			expression = "^"+dayExpression + "[- / ]?" + monthExpression + "[- /]?(18|19|20|21)\\d{2,4}";
		}
		CharSequence inputStr = date;
		Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(inputStr);
		if(matcher.matches()){
			isValid=true;
		}
    }

 


Hope this is useful to all the Java developers. Share your opinions below.

Enjoy.