I'm trying to parse a string date value to date object.
If the provided date is not in the correct format, error has to be thrown.
Trying to achieve this with SimpleDateFormat
, but Its not working as expected.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date d = sdf.parse("01/01/2021anything");
System.out.println(d);
For the above code getting output Fri Jan 01 00:00:00 GMT 2021
where I expected ParseException
. Can someone explain why this happens and how I can validate the above date with SimpleDateFormat
. Appreciate the effort.