Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20)

I have created this funtion to parse date but this gives exception : Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20) . Please help as I am not able to figure out whats wrong with this code.

public Date parseDate() {
    String strDate ="Fri Oct 10 23:11:29 IST 2014";
    String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";
    SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
    try {
        Date date = formatter.parse(strDate);
        return date;
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return null;
}
like image 538
Priya Singhal Avatar asked Dec 09 '14 09:12

Priya Singhal


2 Answers

Use a locale for the parser:

    SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.US);

This should fix your problem. At least it works for me with your example.

EDIT:

It looks like there is indeed a problem with Android and IST timezone. I can parse any time zone on Android using the above pattern, but not IST.

A quick hack is to modify the timezone part, if there is an IST zone in the string. This works for me also on Android:

    String strDate = "Fri Oct 10 23:11:29 IST 2014";
    strDate = strDate.replace(" IST ", " GMT+0530 ");
    String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";

    SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
like image 108
Udo Klimaschewski Avatar answered Oct 21 '22 16:10

Udo Klimaschewski


 SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
 formatter.setLenient(true);

//setting the formatter to be lenient solves the problem

Thanks folks for helping....

like image 44
Priya Singhal Avatar answered Oct 21 '22 15:10

Priya Singhal