时区中的Andr​​oid 2.3的SimpleDateFormat破 [英] TimeZone broken in SimpleDateFormat in android 2.3

查看:136
本文介绍了时区中的Andr​​oid 2.3的SimpleDateFormat破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Andr​​oid 2.3昨晚近日发布。所以很自然我想它我的应用程序,发现有日期格式的问题。我注意到DateFormatter产生不同的格式。所以做一个简单的Java程序:

 ((的SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.LONG,
    。DateFormat.LONG))格式(新的Date());

输出为


  

2010年12月7日上午十一时49分40秒EST


做同样的事情在Android模拟器,你会得到


  

2010年12月7日上午11点42分50秒格林尼治标准​​时间05:00


注意不同的时区。有没有人跑了这个问题?有另一种格式,我可以使用不依赖于Java的实现?

编辑:
好了,所以这里是更详细的,为什么我觉得这是打破:

使用这个code:

 私人最终的DateFormat格式=
    新的SimpleDateFormat(EEE,DD MMM YYYY HH:MM:SS Z);

我试图解析一个日期,但下面的错误被抛出:

  12月12日至7日:55:49.556:ERROR / DateDeserializer(847):在解析时错误日期
    java.text.ParseException:无法解析的日期:星期一,2010年12月6日17点13分35秒EST
    在java.text.DateFormat.parse(DateFormat.java:626)
    在com.currency.mobile.client.DateDeserializer
        .deserialize(DateDeserializer.java:31)
    在com.currency.mobile.client.DateDeserializer
        .deserialize(DateDeserializer.java:19)
    在组织。codehaus.jackson.map.deser.SettableBeanProperty
        .deserialize(SettableBeanProperty.java:149)


解决方案

有什么不对的输出。
您创建一个<一个href=\"http://download.oracle.com/javase/6/docs/api/java/text/DateFormat.html#getDateTimeInstance%28int,%20int%29\"相对=nofollow>日期格式 -instance依赖于默认的区域设置。这是不寻常的是,不同的机器,不同的Java安装的默认语言环境变化等区域设置相关操作的输出。在这种情况下,默认时区是不同,但在你的问题重新present同一日期,印有相同的格式字符串 MMMMM D中的两个输出,YYYY HH:MM:SS AZ

更新:
解析()中的Andr​​oid 2.3将与像时区GMT + XXXX 等,但不承认 EST 的工作例如,作为分析的有效时区。 Android的知道 EST 如果你使用 TimeZone.getTimeZone(EST)

UPDATE2:

三个字母时区的IDEST, HST和MST是pcated德$ p $。不要使用它们。

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)

解决方案

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.

这篇关于时区中的Andr​​oid 2.3的SimpleDateFormat破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆