wei-song wei-song - 1 year ago 82
Android Question

Android SimpleDateFormat 'z' format error

I use codes below, get diffrent result! so how can I fix it?

in 2.3.5 system, will get
Thu, 20 Mar 2014 03:11:56 GMT+00:00

in 4.x system, will get
Thu, 20 Mar 2014 03:13:28 GMT

private static final String RFC822_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";

private static DateFormat getRfc822DateFormat() {
SimpleDateFormat rfc822DateFormat = new SimpleDateFormat(RFC822_DATE_FORMAT, Locale.US);

rfc822DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));

return rfc822DateFormat;

I fix it like this , but why it cause?

public static String formatRfc822Date(Date date)
String format = getRfc822DateFormat().format(date);
//fix with android 2.3.5 Thu, 20 Mar 2014 03:11:56 GMT+00:00
format = format.substring(0, format.lastIndexOf("+"));
return format;

Answer Source

Get the TimeZone as below...


instead of as below...

new SimpleTimeZone(0, "GMT")

Now, pass TimeZone.getTimeZone("GMT+0000") as argument through setTimeZone() method as follows...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download