Java Regular Expression Validate date example shows how to validate date using regex in Java. Example also shows how to validate date using regex for yyyy-mm-dd, yyyy/mm/dd, dd/mm/yyyy, mm/dd/yyyy and other formats.
How to validate date using regular expression (RegEx) in Java?
Basic date validation using regular expression is fairly easy. We need to create a regular expression pattern according to the format we want to validate date against.
Assuming we want to validate the date in yyyy-mm-dd format, we will need 4 digits followed by -, then 2 digits for the month followed by – and then 2 digits for the day. So our pattern will be like \\d{4}-\\d{2}-\\d{2}
. \\d
matches with any digits and {number} means number of times.
Let’s create a sample program with this pattern and see if this works.
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 |
public class JavaRegExDateValidationExample { public static void main(String[] args) { //Dates to validate in yyyy-mm-dd format String[] strDates = { "2018-10-31", "2012-12-12", "2005-3-11", "2009-05-5", "2015-12-28" }; for(String strDate : strDates){ validateDate(strDate); } } private static void validateDate(String strDate){ String strDateRegEx = "\\d{4}-\\d{2}-\\d{2}"; if(strDate.matches(strDateRegEx)){ System.out.println(strDate + " is valid"); }else{ System.out.println(strDate + " is not valid"); } } } |
Output
1 2 3 4 5 |
2018-10-31 is valid 2012-12-12 is valid 2005-3-11 is not valid 2009-05-5 is not valid 2015-12-28 is valid |
It worked for the sample dates we provided. But how about input date string “2015-55-34” or “2015-99-99”? Well, even though the dates are wrong, both of them will pass our date validation regex because it is syntactically correct.
Let’s try to include some rules as well like month cannot be less than 1 and more than 12 and day cannot be less than 1 and more than 31. Here is the updated pattern \\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|[3][01])
where,
\\d{4}
means any 4 digits and will be used to validate the year part of the date. The (0[1-9]|1[012])
means either 0 and any digit between 1 and 9 or 1 followed by 0, 1, or 2. This will take care of our month part and will not allow months less than 01 and more than 12.
The (0[1-9]|[12][0-9]|[3][01])
pattern means any one of (a) 0 and any digit between 1 and 9 (b) 1 or 2 followed by any digit between 0 and 9 (c) 3 followed by either 0 or 1. This part will take care of the day part and will not allow anything less than 01 and more than 31.
Here is the example program using the above-given pattern.
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 |
public class JavaRegExDateValidationExample { public static void main(String[] args) { //Dates to validate in yyyy-mm-dd format String[] strDates = { "2018-10-31", "2012-12-12", "2005-3-11", "2015-55-34", "2015-99-99", "2015-13-23", "2015-12-32", }; for(String strDate : strDates){ validateDate(strDate); } } private static void validateDate(String strDate){ String strDateRegEx = "\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|[3][01])"; if(strDate.matches(strDateRegEx)){ System.out.println(strDate + " is valid"); }else{ System.out.println(strDate + " is not valid"); } } } |
Output
1 2 3 4 5 6 7 |
2018-10-31 is valid 2012-12-12 is valid 2005-3-11 is not valid 2015-55-34 is not valid 2015-99-99 is not valid 2015-13-23 is not valid 2015-12-31 is valid |
Well, looks like we have taken care of the problem we had in the first iteration of the program. Wait, what about “2018-02-29”? February cannot have more than 28 days for a normal year and 29 in the case of the leap year. But It will still pass our date validation regex pattern as the date syntax is valid.
What is the right way to validate date in Java?
As we have seen above, regex can only be useful for checking the dates syntactically. It cannot validate whether the date is actually a valid date or not. That’s where we need to use a class like SimpleDateFormat
to check the validity of the date.
Below given code uses both, regex to check the syntax of the date, and SimpleDateFormat class to check whether the date is actually a valid date or not.
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 |
import java.text.SimpleDateFormat; public class JavaRegExDateValidationExample { public static void main(String[] args) { //Dates to validate in yyyy-mm-dd format String[] strDates = { "2018-10-31", "2012-12-12", "2005-3-11", "2015-55-34", "2015-99-99", "2015-13-23", "2015-12-32", }; for(String strDate : strDates){ validateDate(strDate); } } private static void validateDate(String strDate){ boolean isValid = false; String strDateRegEx = "\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|[3][01])"; if(strDate.matches(strDateRegEx)){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try{ sdf.parse(strDate); isValid = true; }catch(ParseException e){} } System.out.println("Is date [" + strDate + "] valid? " + isValid); } } |
Output
1 2 3 4 5 6 7 |
Is date [2018-10-31] valid? true Is date [2012-12-12] valid? true Is date [2005-3-11] valid? false Is date [2015-55-34] valid? false Is date [2015-99-99] valid? false Is date [2015-13-23] valid? false Is date [2015-12-32] valid? false |
As you can see from the output, this approach worked perfectly. Inside the validateDate method, we assumed that the date is not valid by setting isValid to false. The first step is to check if the date is in a given format i.e. yyyy-mm-dd using the regular expression we used in the earlier code. If it is not, the date is not valid.
Inside the if condition, we checked the date validity by actually parsing it using parse
method of SimpleDateFormat class. If the date is invalid, the parse
method throws ParseException
and isValid variable remains false. If the date is valid, no exception is thrown and isValid is set to true in the next statement.
It may be possible to validate the date entirely using the regular expression without using SimpleDateFormat, but it will not be worth the effort.
Use below given regex patterns If you want to validate the date using a format different from yyyy-mm-dd.
1) dd-mm-yyyy
1 |
(0[1-9]|[12][0-9]|[3][01])-(0[1-9]|1[012])-\\d{4} |
2) dd/mm/yyyy
1 |
(0[1-9]|[12][0-9]|[3][01])/(0[1-9]|1[012])/\\d{4} |
3) dd/mm/yy
1 |
(0[1-9]|[12][0-9]|[3][01])/(0[1-9]|1[012])/\\d{2} |
4) dd-mm-yy
1 |
(0[1-9]|[12][0-9]|[3][01])-(0[1-9]|1[012])-\\d{2} |
5) mm-dd-yyyy
1 |
(0[1-9]|1[012])-(0[1-9]|[12][0-9]|[3][01])-\\d{4} |
6) mm/dd/yyyy
1 |
(0[1-9]|1[012])/(0[1-9]|[12][0-9]|[3][01])/\\d{4} |
7) mm-dd-yy
1 |
(0[1-9]|1[012])-(0[1-9]|[12][0-9]|[3][01])-\\d{2} |
8) mm/dd/yy
1 |
(0[1-9]|1[012])/(0[1-9]|[12][0-9]|[3][01])/\\d{2} |
9) yyyy/mm/dd
1 |
\\d{4}/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|[3][01]) |
10) yyyy/mmm/dd
1 |
\\d{4}/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/(0[1-9]|[12][0-9]|[3][01]) |
11) yyyy-mmm-dd
1 |
\\d{4}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(0[1-9]|[12][0-9]|[3][01]) |
12) dd-mmm-yyyy
1 |
(0[1-9]|[12][0-9]|[3][01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{4} |
13) dd/mmm/yyyy
1 |
(0[1-9]|[12][0-9]|[3][01])/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/\\d{4} |
14) dd-mmm-yy
1 |
(0[1-9]|[12][0-9]|[3][01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{2} |
15) dd/mmm/yy
1 |
(0[1-9]|[12][0-9]|[3][01])/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/\\d{2} |
16) yyyymmdd
1 |
\\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|[3][01]) |
17) yyyyddmm
1 |
\\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|[3][01]) |
18) ddmmyyyy
1 |
(0[1-9]|[12][0-9]|[3][01])(0[1-9]|1[012])\\d{4} |
19) mmddyyyy
1 |
(0[1-9]|1[012])(0[1-9]|[12][0-9]|[3][01])\\d{4} |
20) yyyy mm dd
1 |
\\d{4} (0[1-9]|1[012]) (0[1-9]|[12][0-9]|[3][01]) |
If the above list does not include the format you want to validate, you can easily modify any of the above-given regex to suit your needs. Also, do not forget to change the date format in the SiImpleDateFormat constructor.
Please also see how to extract date from string using regex example.
If you like this example, you will also like String in Java and regular expression tutorial with examples.