Java Date and Time RegEx

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 date in yyyy-mm-dd format, we will need 4 digits followed by -, then 2 digits for month followed by – and then 2 digits for 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 year part of 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 month 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 above given pattern.

Output

Well, looks like we have taken care of the problem we had in 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 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 valid date or not. That’s where we need to use class like SimpleDateFomat 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 valid date or not.

Output

As you can see from the output, this approach worked perfectly. Inside the validateDate method, we assumed that date is not valid by setting isValid to false. The first step is to check if the date is in given format i.e. yyyy-mm-dd using regular expression we used in earlier code. If it is not, date is not valid.

Inside the if condition, we checked the date validity by actually parsing it using parse method of SimpleDateFormat class. If date is invalid, parse method throws ParseException and isValid variable remains false. If date is valid, no exception is thrown and isValid is set to true in the next statement.

It may be possible to validate 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 date using 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 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.

If you like this example, you will also like Java String examples and regular expression examples.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *