Below is my code for parsing a date using SimpleDateFormat with a template:
String pattern = "yyyy-MM-dd"; SimpleDateFormat format = new SimpleDateFormat(pattern); try { Date date = format.parse("05-21-2030"); System.out.println(date); } catch (ParseException e) { e.printStackTrace(); }
You can see that the date I passed for parsing is different from the date format specified in SimpleDateFormat. In this case, I was expecting an excpetion, as the format is different, but it successfully dealt with some different date values. I got the result - Tue March 22 00:00:00 IST 12
When I transmit the same format as 2030-05-21 , it works fine.
Can you guys tell me how I can prevent such things in my code?
source share