Java RegEx – Validate dd/mm/yyyy Date Format example shows how to validate date in dd/mm/yyyy, dd-mm-yyyy, or mm/dd/yyyy format using regular expression.
How to validate dd/mm/yyyy date format using regex in Java?
The structure of the date in the format “dd/mm/yyyy” needs to have 2 digits as day followed by /, followed by 2 digits for the month followed by /, and then 4 digits for the year. Let’s try to come up with a simple regex to validate this.
Here is the basic version of the same.
1 |
^\\d{2}/\\d{2}/\\d{4}$ |
Where,
1 2 3 4 5 6 7 8 9 10 |
^ - start of the string \\d - matches any number (0 to 9) {2} - 2 times for day / - separator \\d - matches any number (0 to 9) {2} - 2 times for month / - separator \\d - matches any number (0 to 9) {4} - 4 times for year $ - end of the string |
Let’s try our regular expression against some dates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class ValidateDDMMYYYYDate { public static void main(String[] args) { String[] dates = { "1/01/1999", "01/1/1999", "01/01/199", "01/01/20011", "01/01/1990", }; String strPattern = "^\\d{2}/\\d{2}/\\d{4}$"; for(String strDate : dates) { System.out.println(strDate + " => " + strDate.matches(strPattern)); } } } |
Output
1 2 3 4 5 |
1/01/1999 => false 01/1/1999 => false 01/01/199 => false 01/01/20011 => false 01/01/1990 => true |
It looks like it is working for a basic test scenario. However, what about the date “45-15-2001”? Our regex will consider this date as a valid date because syntax-wise it is still valid even though the day cannot be greater than 31 and the month cannot be greater than 12.
Let’s include these additional rules in our regex. Here is the updated regular expression.
1 |
(0[1-9]|[12][0-9]|[3][01])/(0[1-9]|1[012])/\\d{4} |
This regex is divided into 3 parts. The first part is for the day. Which says,
1 2 3 4 5 6 7 8 9 |
(0[1-9]|[12][0-9]|[3][01]) Where, 0[1-9] - first digit is 0 and second digit is any from 1 to 9 | - OR [12][0-9] - first digit is either 1 or 2 and second digit is any from 0 to 9 | - OR [3][01] - first digit is 3 and second digit is either 0 or 1 |
This regex allows us to have days from 01 to 31. The second part is for the month.
1 2 3 4 5 6 7 |
(0[1-9]|1[012]) where 0[1-9] - the first digit is 0 and second digit is any from 1 to 9 | OR 1[012] - the first digit is 1 and second digit is any from 0, 1, or 2 |
This allows us to have the month from 01 to 12. The third, year part is simply “\\d{4}”, which says any 4 digits. All three parts are connected using “/” to make the date in the format “dd/mm/yyyy”. Let’s test the new regex.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class ValidateDDMMYYYYDate { public static void main(String[] args) { String[] dates = { "1/01/1999", "01/1/1999", "01/01/199", "01/01/20011", "32/01/2000", "31/15/2000", "00/01/2000", "01/00/2000", "01/01/2001" }; String strPattern = "^(0[1-9]|[12][0-9]|[3][01])/(0[1-9]|1[012])/\\d{4}$"; for(String strDate : dates) { System.out.println(strDate + " => " + strDate.matches(strPattern)); } } } |
Output
1 2 3 4 5 6 7 8 9 |
1/01/1999 => false 01/1/1999 => false 01/01/199 => false 01/01/20011 => false 32/01/2000 => false 31/15/2000 => false 00/01/2000 => false 01/00/2000 => false 01/01/2001 => true |
As you can see from the output, it worked as expected. It rejected the dates having the day greater than 31 or less than 1. It also rejected the month greater than 12 and less than 1.
Important Note:
Even though the date is in a valid format as per the above regex, it does not mean that the actual date is valid. For example, the format of the date “30/02/2001” is valid, but the actual date is not because February month cannot have day 30.
In order to do that, we need to actually parse the date after it is validated by the regex as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import java.text.ParseException; import java.text.SimpleDateFormat; public class ValidateDDMMYYYYDate { public static void main(String[] args) { String[] dates = { "1/01/1999", "01/1/1999", "01/01/199", "01/01/20011", "32/01/2000", "31/15/2000", "00/01/2000", "01/00/2000", "30/02/2001", "01/01/2001" }; for(String strDate : dates) { System.out.println( strDate + " => " + isDateValid(strDate)); } } private static boolean isDateValid(String strDate) { boolean blnValid = false; String strPattern = "^(0[1-9]|[12][0-9]|[3][01])/(0[1-9]|1[012])/\\d{4}$"; //if date syntax is valid if(strDate.matches( strPattern )) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); //to reject invalid dates sdf.setLenient(false); //parse the actual date try { //throws ParseException in case of invalid date sdf.parse(strDate); //at this point the date is valid blnValid = true; }catch(ParseException pe) { //do nothing } } return blnValid; } } |
Output
1 2 3 4 5 6 7 8 9 10 |
1/01/1999 => false 01/1/1999 => false 01/01/199 => false 01/01/20011 => false 32/01/2000 => false 31/15/2000 => false 00/01/2000 => false 01/00/2000 => false 30/02/2001 => false 01/01/2001 => true |
How to validate mm/dd/yyyy date format using regex in Java?
In the above code replace the pattern with this one.
1 |
^(0[1-9]|1[012])/(0[1-9]|[12][0-9]|[3][01])/\\d{4}$ |
You can rearrange the day, month, and year parts to suit your needs. If you want more formats, please visit the java regex validate date example.
This example is part of the Java RegEx tutorial with examples.
Please let me know your thoughts in the comment section below.