DateFormat giving wrong answer

Viewed 325

I was using the below code to format a date. But it is giving unexpected result when I give data in incorrect 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, output is - formattedVal : 0009-02-05.

Instead of throwing the Parse exception, it is parsing the value and giving me an incorrect output. Can someone please help me to understand this anomalous behavior.

4 Answers
Related