I am trying to use the SimpleDateFormat class to validate a date entered by a user in a web app. Unfortunately, when I test it by putting an invalid date or month, it still computes the date and returns it as a valid date. Here is a test code to prove my point:
public static void main(String[] args) { //String pattern = "dd/MM/yyyy"; String pattern = "MM/dd/yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date nDate; String value = "100/26/2006"; System.out.println("**************DATE: " + value); try { nDate = sdf.parse(value); System.out.println("************DATE AFTER PARSING: " + nDate); } catch (ParseException ex) { System.out.println(ex); ex.printStackTrace(); } }
Can somebody point me the mistake I am making? I was expecting an exception to be thrown.