What happens if someone provides a five digit year?
My earlier post on how to validate email address, SSN and phone number validation using Java regex still attracts lot of visitors.
Today I realized that another piece of data that many programmers need to validate is the date.
The logic is same but the elements are re-arranged.
You can use following method to switch between American and English date formats dynamically at runtime based on an additional boolean parameter.
However, I'm strongly against rejecting five or more digits, as that essentially puts an expiration date on your code.
It may be early, but why introduce an unnecessary Y10K problem? Your many catch-clauses provide a detailed message, but does anyone need it? a single catch clause stating "expected MM/DD/YYYY, got ..." should do.
User might enter date in format “dd/mm/yyy” or “dd-mm-yyyy” or “yy”! It runs through each format from the list and check weather input string is valid date or not.
If it fails to convert String to Date then it returns null.
// updated via the `Scheduled Thread Pool Executor` private int year; private int month; private int day; private static final Pattern DATE_PATTERN = Pattern.compile("\s*(\d)/(\d)/(\d))\s*"); public static boolean is Valid Date(String date) As already said, solution reading the input char by char would be faster, but I'd go the opposite direction.