Unsurpassed date: "Fri 10 Oct 23:11:07 IST 2014" (on an offset basis of 20)

I created this funtion for parsing a date, but this gives an exception: Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (offset 20). Please help as I cannot understand what is 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;
}
+4
source share
5 answers
 SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
 formatter.setLenient(true);

// setting the formatter to be soft solves the problem

Thank you for helping ....

+1
source

Use the locale for the parser:

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

This should solve your problem. At least this works for me with your example.

EDIT:

, Android IST. Android, , IST.

- , IST. 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);
+6

, . , IST,

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

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

z z .

(Java SE 1.8.0), z z .

Android007 : " Java , Android " - , , Oracle J2SE:)

. : fooobar.com/questions/14552/...

0

. SimpleDateFormat (, , , ... ..), , .

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

Change your formatter as follows:

SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
0
source

All Articles