Date validation is a common task in Java that deal with validating a date entered by a user, or a date extracted from a file or database. This post will discuss various ways to validate a date in Java.

Date validation involves checking whether a given date is in a valid format or within a valid range. For example, suppose we have a string "07-01-2013" that represents a date in the format "dd-MM-yyyy". There are several ways to validate dates in Java:

1. Using OWASP Validator

We can use OWASP Validation to check for a US format date, which extends support for leap years. It uses the following regular expression:

^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

To use this regular expression to validate dates, we need to import the java.util.regex package and create a Pattern object with the desired regular expression that matches the date format. Then create a Matcher object by applying the pattern to the date string and use the matches method of the matcher to check if the date string matches the pattern. For example:

Download  Run Code

Output:

01/15/2010 is valid
1/15/2010 is valid
1-15-2010 is valid
1/15/00 is valid
02/29/2016 is valid
02/29/2015 isn’t valid

2. Using Apache Commons DateValidator

Instead of using regex for date validation, we can also use external libraries that provide more features and flexibility for date validation. Apache Commons Validator provides a simple and convenient interface called DateValidator for validating dates in various formats and locales. To validate a date, we need to get a singleton instance of DateValidator that can validate dates using the DateValidator.getInstance() method. Then use the isValid() or validate() method to validate the given string according to the desired date format and strictness. The syntax is as follows:

boolean isValid(value)
boolean isValid(value, pattern)
boolean isValid(value, Locale)
boolean isValid(value, pattern, Locale)
 
Date validate(value)
Date validate(value, pattern)
Date validate(value, Locale)
Date validate(value, pattern, Locale)

Here is an example of how to use these methods to validate a date in Java:

Download Code

Output:

01/12/2010 is valid
1/15/2010 is valid
1-15-2010 isn’t valid
02/29/2016 is valid
02/29/2015 isn’t valid

 
The advantage of using this method is that it is convenient and reliable. It uses only one method call to validate a date in Java. It also supports different date formats and locales. However, it requires adding an external dependency to our project, which may increase the size or complexity of our project.

3. Using DateFormat and SimpleDateFormat

To use DateFormat and SimpleDateFormat to validate dates, we need to create a SimpleDateFormat object with the desired format pattern and use the parse method of the formatter to parse the date string into a Date object. Then use the format method of the formatter to format the Date object back into a string and compare the formatted string with the original string to see if they are equal. Here is an example of how to do this in Java:

Download  Run Code

Output:

01/15/2010 is valid
1/15/2010 isn’t valid
1-15-2010 isn’t valid
02/29/2016 is valid
02/31/2015 isn’t valid

 
Note that it does not handles the cases where the day and month of the date string is not 2 digit. This method returns false for valid date “1/15/2010” and true for “01/15/2010”.

That’s all about validating a date in Java.