سؤال

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.

نصائح أخرى

Both are same TimeZone just different representation

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

Evgueni

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top