I wanted to know that is there any method available in Java that can do this.Otherwise I may go for Regex solution.
I have input string from user that can be any characters. And I want to check that the input string is according to my required date format or not.
As I have input 20130925 and my required format is dd/MM/yyyy so, for this case I should get false.
I don't want to convert this date I just want to check whether input string is according to required date format or not.
I have tried following
Date date = null;
try {
date = new SimpleDateFormat("dd/MM/yyyy").parse("20130925");
} catch (Exception ex) {
// do something for invalid dateformat
}
but my catch (Exception ex) block is unable to catch any exceptions generated by SimpleDateFormat.Parse();
For your case, you may use regex:
For a larger scope or if you want a flexible solution, refer to MadProgrammer's answer.
Edit
Almost 5 years after posting this answer, I realize that this is a stupid way to validate a date format. But i'll just leave this here to tell people that using regex to validate a date is unacceptable
formatter.setLenient(false) will enforce strict matching.
If you are using Joda-Time -