I am using parse function for date validation to parse the valid format of the date "mm-dd-yyyy". Now if I give the input date is "04-31-2008", which is a valid format but invalid date for the April month as we don't have 31st in April, parse function throws an exception but I want to catch this exception before It parses the date.In short I want to separate this two validation for Invalid Format and Invalid date. And I am unable to figure out how can I do this?
You can use another form of parse that will not throw an exception:
I have used this myself to make sure that there is no "clutter" after the valid string, e.g. "04-30-2009abc":
pos.getIndex() is guaranteed to not change if there is an error, so in this case it will remain 0.