Skip to content

Java Regular Expression Validate Date Example (RegEx)

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.

Output

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.

Output

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.

Output

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

2) dd/mm/yyyy

3) dd/mm/yy

4) dd-mm-yy

5) mm-dd-yyyy

6) mm/dd/yyyy

7) mm-dd-yy

8) mm/dd/yy

9) yyyy/mm/dd

10) yyyy/mmm/dd

11) yyyy-mmm-dd

12) dd-mmm-yyyy

13) dd/mmm/yyyy

14) dd-mmm-yy

15) dd/mmm/yy

16) yyyymmdd

17) yyyyddmm

18) ddmmyyyy

19) mmddyyyy

20) yyyy mm dd

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.

About the author

Leave a Reply

Your email address will not be published.