5

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;
}
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
Priya Singhal
  • 1,261
  • 11
  • 16

5 Answers5

6

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);
Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
  • Yes, on my Android, too. See my edited answer with a possible fix. – Udo Klimaschewski Dec 09 '14 at 11:12
  • This is a terrible solution. What if the date is in a different timezone such as Fri Oct 10 23:11:29 PDT 2014 or other? Is he suppose to string replace for every possible timezone possibility? – portfoliobuilder Aug 23 '16 at 18:16
  • @portfoliobuilder The problem only exists, when IST is specified in the string. Every other timezone works. `Fri Oct 10 23:11:29 PDT 2014 ` will be converted correctly, as all other timezones will be, too. Replacing a possible IST timezone with it's equivalent GMT+0530 will work as a workaround. – Udo Klimaschewski Aug 24 '16 at 11:33
2
 SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
 formatter.setLenient(true);

//setting the formatter to be lenient solves the problem

Thanks folks for helping....

Priya Singhal
  • 1,261
  • 11
  • 16
0

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;

}
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

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

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
0

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);
alienchow
  • 383
  • 2
  • 7
  • I think @alienchow is right! You should set the Local, plus also set formatter.Lenient(true) AND set formatter.setTimeZone(TimeZone.getDefault() and use the pattern 'EEE MMM dd hh:mm:ss Z yyyy' and you should not have parse error anymore. – portfoliobuilder Aug 23 '16 at 19:00