I'm just wondering if there is a way (maybe with regex) to validate that an input on a Java desktop app is exactly an string formated as: "YYYY-MM-DD".
I've searched but with no success.
Thank you
I'm just wondering if there is a way (maybe with regex) to validate that an input on a Java desktop app is exactly an string formated as: "YYYY-MM-DD".
I've searched but with no success.
Thank you
java.time
The proper (and easy) way to do date/time validation using Java 8+ is to use the
java.time.format.DateTimeFormatter
class. Using a regex for validation isn't really ideal for dates. For the example case in this question:This code will parse the text, validate that it is a valid date, and also return the date as a
LocalDate
object. Note that theDateTimeFormatter
class has a number of static predefined date formats matching ISO standards if your use case matches any of them.Use the following regular expression:
as in
With the
matches
method, the anchors^
and$
(beginning and end of string, respectively) are present implicitly.I would go with a simple regex which will check that days doesn't have more than 31 days and months no more than 12. Something like:
This is the format "dd-MM-yyyy". You can tweak it to your needs (for example take off the ? to make the leading 0 required - now its optional), and then use a custom logic to cut down to the specific rules like leap years February number of days case, and other months number of days cases. See the DateChecker code below.
I am choosing this approach since I tested that this is the best one when performance is taken into account. I checked this (1st) approach versus 2nd approach of validating a date against a regex that takes care of the other use cases, and 3rd approach of using the same simple regex above in combination with SimpleDateFormat.parse(date).
The 1st approach was 4 times faster than the 2nd approach, and 8 times faster than the 3rd approach. See the self contained date checker and performance tester main class at the bottom. One thing that I left unchecked is the joda time approach(s). (The more efficient date/time library).
Date checker code:
Date checking/testing and performance testing:
Some useful notes:
- to enable the assertions (assert checks) you need to use -ea argument when running the tester. (In eclipse this is done by editing the Run/Debug configuration -> Arguments tab -> VM Arguments -> insert "-ea"
- the regex above is bounded to years 1800 to 2199
- you don't need to use ^ at the beginning and $ at the end to match only the whole date string. The String.matches takes care of that.
- make sure u check the valid and invalid cases and change them according the rules that you have.
- the "only whole string" version of each regex gives the same speed as the "normal" version (the one without ^ and $). If you see performance differences this is because java "gets used" to processing the same instructions so the time lowers. If you switch the lines where the "normal" and the "only whole string" version execute, you will see this proven.
Hope this helps someone!
Cheers,
Despot
Construct a SimpleDateFormat with the mask, and then call: SimpleDateFormat.parse(String s, ParsePosition p)
This will do it regex:
"^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$"
This will take care of valid formats and valid dates. It will not validate the correct days of the month i.e. leap year.Good luck!