Validating date format in java directory of online dating services
09-Nov-2019 06:59
The expression for day validation is a group of three mutually exclusive sub-expressions or alternatives. This expression will validate dates in US format from years 1800 to 2199.For example 03/15/2009, 10/12/1885 are valid dates but the date 31/01/1999 will not pass validation.You can use following method to switch between American and English date formats dynamically at runtime based on an additional boolean parameter.I'm Nataraja Gootooru, programmer by profession and passionate about technologies. Simple Date Format class provides methods to format and parse date and time in java.
I included the explanation on how the regular expression is constructed in the code, however the day part of the date needs a little more explanation. This is done to verify that the combination of the digits in the day string falls between 01 and 31.
For date in European (English) format, we can rearrange the regular expression as follows. So the date 17/08/2009 is valid but 08/17/2009 will not pass this validation.
The logic is same but the elements are re-arranged.
* So, for this implementation, we prepare an array that contains the formats against which we will * perform validation test.
* Next, we loop the entire array and test against each format.
Many developers have commented on this post about the issue of Thread safety in this code snippet.