TimeZone broken in SimpleDateFormat in android 2.3

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

Android 2.3 was recently released last night. So naturally I tried my app on it and found there was date formatting issue. I have noticed the DateFormatter produces different formats. So do this in a simple Java program:

((SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.LONG,      DateFormat.LONG)).format(new Date()); 

Output is

December 7, 2010 11:49:40 AM EST

Do the same thing in an android emulator and you get

December 7, 2010 11:42:50 AM GMT-05:00

Notice the different time zone. Has anybody ran in to this issue? Is there another formatter I can use that doesn't depend on Java's implementation?

EDIT: Ok so here is more detail to why I think this is broken:

Using this code:

private final DateFormat format =      new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); 

I tried to parse a date but the following error is thrown:

12-07 12:55:49.556: ERROR/DateDeserializer(847): Error when parsing date     java.text.ParseException: Unparseable date: "Mon, 06 Dec 2010 17:13:35 EST"     at java.text.DateFormat.parse(DateFormat.java:626)     at com.currency.mobile.client.DateDeserializer         .deserialize(DateDeserializer.java:31)     at com.currency.mobile.client.DateDeserializer         .deserialize(DateDeserializer.java:19)     at org.codehaus.jackson.map.deser.SettableBeanProperty         .deserialize(SettableBeanProperty.java:149) 

回答1:

There is nothing wrong with the output. You create a DateFormat-Instance which depends on the default Locale. It is not unusual that on different machines, different java-installations the default Locale vary and so the output of locale-dependent operations. In this case the default TimeZone is different, but the two outputs in your question represent the same Date, printed with the same format String MMMMM d, yyyy hh:mm:ss a z.

UPDATE: parse() in Android 2.3 will work with TimeZones like GMT+xxxx etc, but it doesn't recognize EST for example as a valid TimeZone for parsing. Android knows about EST if you use TimeZone.getTimeZone("EST").

UPDATE2:

Three-letter timezone IDs "EST", "HST", and "MST" are deprecated. Do not use them.



回答2:

Both are same TimeZone just different representation



回答3:

Looks like support for pattern "EEE MMM dd HH:mm:ss zzz yyyy" is also broken.

Evgueni



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!