Calendar set date causing exception with message "Unparsable date"

246 Views Asked by At

My code to get the calendar date frone the string "2018-04-14'T'15:38:14", it is currently causing a exception WHEN i call setTime. The code

    Calendar cal = Calendar.getInstance();
    // format of date yyyy-MM-dd'T'HH:mm:ss
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd 'T' 
    cal.setTime(sdf.parse("2018-04-14'T'15:38:14"));// all done1 

The above line causes a exception saying "Unparsable date"

1

There are 1 best solutions below

1
On

'T' is a constant. Notice the subtle difference. The format is defined with T in single quotes but when you pass the value, it's without.

Calendar cal = Calendar.getInstance();
// format of date yyyy-MM-dd'T'HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
cal.setTime(sdf.parse("2018-04-14T15:38:14"));// all done1