I used the code below to format the date. But this gives an unexpected result when I give the data in the wrong format.
DateFormat inputFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");`
String dateVal = "3/8/2016 12:00:00 AM";
try {
Date date = inputFormat.parse(dateVal);
String formattedVal = outputFormat.format(date);
System.out.println("formattedVal : "+formattedVal);
} catch (ParseException pe) {
throw pe;
}
In the above case, the output is formattedVal: 0009-02-05.
Instead of throwing a Parse exception, it parses the value and gives me the wrong conclusion. Can someone please help me understand this abnormal behavior.
source
share