wei-song wei-song - 26 days ago 9
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
if(!format.endsWith("GMT")){
format = format.substring(0, format.lastIndexOf("+"));
}
return format;
}

Answer

Get the TimeZone as below...

TimeZone.getTimeZone("GMT+0000")

instead of as below...

new SimpleTimeZone(0, "GMT")

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

rfc822DateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Comments