Java RegEx to validate Date – dd/mm/yyyy pattern

In this tutorial we are going to see about how to validate date in dd/mm/yyyy pattern with leading zero required in date and month.

Regular Expression for Date

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

JavaDataRegEx.java

package com.ehowtonow.regex;

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

public class JavaDataRegEx {

	public static void main(String[] args) {

		List<String> javaDates = new ArrayList();
		
		javaDates.add("01/03/2019");
		
		javaDates.add("30/03/2019");
		javaDates.add("31/03/2019");
		javaDates.add("32/03/2019");
		
		javaDates.add("03/12/2019");
		javaDates.add("03/13/2019");
		
		javaDates.add("01/1/2019");
		javaDates.add("6/12/2019");
		javaDates.add("1/2/19");
		javaDates.add("01/02/19");
		javaDates.add("7/12/19");
		javaDates.add("15/3/19");
		
		javaDates.add("5/13/19");
		javaDates.add("2005/13/1");
		javaDates.add("200/13/1");
		//javaDates.add("20/JUN/2001");
		javaDates.add("30/02/3019");
		javaDates.add("30/02/2019");
		javaDates.add("31/04/2019");

		// RegEx to validate dd/mm/yyyy with leading zero in date
		String dateRegex = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";

		Pattern pattern = Pattern.compile(dateRegex);

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

}

Output

01/03/2019 isValid ? : true
30/03/2019 isValid ? : true
31/03/2019 isValid ? : true
32/03/2019 isValid ? : false
03/12/2019 isValid ? : true
03/13/2019 isValid ? : false
01/1/2019 isValid ? : false
6/12/2019 isValid ? : false
1/2/19 isValid ? : false
01/02/19 isValid ? : false
7/12/19 isValid ? : false
15/3/19 isValid ? : false
5/13/19 isValid ? : false
2005/13/1 isValid ? : false
200/13/1 isValid ? : false
30/02/3019 isValid ? : true
30/02/2019 isValid ? : true
31/04/2019 isValid ? : true

If you see the output closely its return true for date 30/02/2019 which is actually wrong. there is no 30 in February. This regular expression validate the format alone, each month will have different number of days. If you give 31/04/2019 also it will return true.

The worst part is new Date(date) also will not throw any error (Note : new Date(String date) is deprecated ). If you use SimpleDateFormat(“dd/MM/yyyy”).parse(date) it will shift the dates to next month. See the example below.

package com.ehowtonow.regex;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaDataRegEx {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {

		List<String> javaDates = new ArrayList();

		javaDates.add("01/03/2019");

		javaDates.add("30/03/2019");
		javaDates.add("31/03/2019");
		javaDates.add("32/03/2019");

		javaDates.add("03/12/2019");
		javaDates.add("03/13/2019");

		javaDates.add("01/1/2019");
		javaDates.add("6/12/2019");
		javaDates.add("1/2/19");
		javaDates.add("01/02/19");
		javaDates.add("7/12/19");
		javaDates.add("15/3/19");

		javaDates.add("5/13/19");
		javaDates.add("2005/13/1");
		javaDates.add("200/13/1");
		// javaDates.add("20/JUN/2001");
		javaDates.add("30/02/3019");
		javaDates.add("30/02/2019");
		javaDates.add("31/04/2019");

		// RegEx to validate dd/mm/yyyy with leading zero in date
		String dateRegex = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";

		Pattern pattern = Pattern.compile(dateRegex);

		for (String date : javaDates) {
			Matcher matcher = pattern.matcher(date);
			System.out.println(date + " isValid ? : " + matcher.matches());
			try {
				Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse(date);
				System.out.println(date + "\t" + date1);
				System.out.println(new Date(date));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

}

Output

01/03/2019 isValid ? : true
01/03/2019 Fri Mar 01 00:00:00 IST 2019
Thu Jan 03 00:00:00 IST 2019
30/03/2019 isValid ? : true
30/03/2019 Sat Mar 30 00:00:00 IST 2019
Thu Jun 03 00:00:00 IST 2021
31/03/2019 isValid ? : true
31/03/2019 Sun Mar 31 00:00:00 IST 2019
Sat Jul 03 00:00:00 IST 2021
32/03/2019 isValid ? : false
32/03/2019 Mon Apr 01 00:00:00 IST 2019
Tue Aug 03 00:00:00 IST 2021
03/12/2019 isValid ? : true
03/12/2019 Tue Dec 03 00:00:00 IST 2019
Tue Mar 12 00:00:00 IST 2019
03/13/2019 isValid ? : false
03/13/2019 Fri Jan 03 00:00:00 IST 2020
Wed Mar 13 00:00:00 IST 2019
01/1/2019 isValid ? : false
01/1/2019 Tue Jan 01 00:00:00 IST 2019
Tue Jan 01 00:00:00 IST 2019
6/12/2019 isValid ? : false
6/12/2019 Fri Dec 06 00:00:00 IST 2019
Wed Jun 12 00:00:00 IST 2019
1/2/19 isValid ? : false
1/2/19 Wed Feb 01 00:00:00 IST 19
Wed Jan 02 00:00:00 IST 2019
01/02/19 isValid ? : false
01/02/19 Wed Feb 01 00:00:00 IST 19
Wed Jan 02 00:00:00 IST 2019
7/12/19 isValid ? : false
7/12/19 Thu Dec 07 00:00:00 IST 19
Fri Jul 12 00:00:00 IST 2019
15/3/19 isValid ? : false
15/3/19 Wed Mar 15 00:00:00 IST 19
Tue Mar 03 00:00:00 IST 2020
5/13/19 isValid ? : false
5/13/19 Fri Jan 05 00:00:00 IST 20
Mon May 13 00:00:00 IST 2019
2005/13/1 isValid ? : false
2005/13/1 Tue Jun 28 00:00:00 IST 7
Sun Jan 01 00:00:00 IST 2006
200/13/1 isValid ? : false
200/13/1 Wed Jul 19 00:00:00 IST 2
Thu Jan 01 00:00:00 IST 201
30/02/3019 isValid ? : true
30/02/3019 Tue Mar 02 00:00:00 IST 3019
Sat Jun 02 00:00:00 IST 3021
30/02/2019 isValid ? : true
30/02/2019 Sat Mar 02 00:00:00 IST 2019
Wed Jun 02 00:00:00 IST 2021
31/04/2019 isValid ? : true
31/04/2019 Wed May 01 00:00:00 IST 2019
Sun Jul 04 00:00:00 IST 2021

How to to handle this scenario ? Will see in our next article.


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to replace all occurrences of a string using RegEx
  2. How to validate Username using Java Regular Expression
  3. How to validate password using Java Regular Expression
  4. How to validate IPv4 Address using Java Regular Expression
  5. How to validate IPv4 Address with Port using Java Regular Expression
  6. How to validate MAC Address using Java Regular Expression
  7. How to validate Email Address using Java Regular Expression
  8. Java RegEx to validate Date – dd/mm/yyyy pattern

Be the first to comment

Leave a Reply

Your email address will not be published.


*