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

2.7k views Asked by At

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;
}
5

There are 5 answers

1
Priya Singhal On BEST ANSWER
 SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
 formatter.setLenient(true);

//setting the formatter to be lenient solves the problem

Thanks folks for helping....

1
alienchow On

Your date pattern contains locale sensitive patterns. Since SimpleDateFormat supports the parsing of dates in different locales (ENGLISH, FRENCH, US, JAPANESE... etc.), you'd have to specify which one you are using.

Judging from your "IST", your current default locale on your computer is probably not ENGLISH or US. Since the string "Fri" and "Oct" are in English, the parsing will fail with your default locale.

Edit your formatter to the following and it should work:

SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
3
Udo Klimaschewski On

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);
2
Meenal On

use this code it will work.i think there is a problem with IST, it is not recognizing it

public Date parseDate(String strDate) {
    strDate = "Fri Oct 10 23:11:29 2014";
    String newPattern = "EEE MMM dd HH:mm:ss yyyy";
    SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
    formatter.setTimeZone(TimeZone.getTimeZone("IST"));
    Date date;
    try {
        date = formatter.parse(strDate.trim());
        Log.i("minal", "date:" + date);
        return date;
    } catch (java.text.ParseException e) {
        e.printStackTrace();
        Log.d("Populace", "ParseException: " + e.getLocalizedMessage());
    }
    return null;

}
0
Honza Zidek On

According to the documentation http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html,

Letter  Date or Time Component  Presentation    Examples
z   Time zone   General time zone   Pacific Standard Time; PST; GMT-08:00
Z   Time zone   RFC 822 time zone   -0800
X   Time zone   ISO 8601 time zone  -08; -0800; -08:00

you are supposed to use small z instead of capital Z for the general time zone.

However, I tried it (Java SE 1.8.0) and it works both with z as well as Z for me.

As Android007 wrote as a comment: "In Java app its working fine but on Android giving exception" - it may be that your library implements the contract more thoroughly than the standard Oracle J2SE :)

See also here: https://stackoverflow.com/a/2581073/2886891