Search code examples
javatype-conversiondatetime-conversion

Error while parsing string of time to calendar


Im using the following code to convert from string with value 09:10:06 but in the cal set time I got exception: Unparseable date: "09:19:06" how can I overcome this issue.

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
Calendar cal  = Calendar.getInstance();
try {
cal.setTime(df.parse(memberValue));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Solution

  • The problem is you're trying to parse an hour with a wrong pattern. Here is the fix.

    DateFormat df = new SimpleDateFormat("HH:mm:ss");