Java TimeZone Conversion

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Java TimeZone Conversion

Steve Livengood
Hi,

I've run into a problem using the Dalvik VM (Android) with the time zone
conversion functions. Specifically, the implementation of
DateTimeZone.forTimeZone() fails when the Java timezone is a GMT+/-hh:mm
format if the current locale is not a western locale. You can easily see the
failure by using this code snippet:

    Locale.setDefault(Locale.CHINA);
    TimeZone sTimeZone = java.util.TimeZone.getTimeZone("GMT-01:00");
    System.out.println(Locale.getDefault());
    System.out.println(sTimeZone.getDisplayName());
    DateTimeZone dtz = DateTimeZone.forTimeZone(sTimeZone);
    System.out.println(dtz.getID());

The invocation of DateTimeZone.forTimeZone() will throw an
IllegalArgumentException when using a VM that localizes time zones for
display purposes (specifically, when using the Dalvik VM).

The code in question is:

  public static DateTimeZone forTimeZone(TimeZone zone) {

    .... other code here that converts named time zones ....

    // Support GMT+/-hh:mm formats
    if (convId == null) {
        convId = zone.getDisplayName();
        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
            convId = convId.substring(3);
            int offset = parseOffset(convId);
            if (offset == 0L) {
                return DateTimeZone.UTC;
            } else {
                convId = printOffset(offset);
                return fixedOffsetZone(convId, offset);
            }
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is
not recognised");

Specifically, the line, convId = zone.getDisplayName(). This returns the
locale-specific display name for the time zone. In the Sun VM, when the time
zone is a custom GMT+nn:nn or GMT-nn:nn, the display name is also of this
same form for every locale. However, in the Dalvik VM, it is properly
localized. For example, when the local is zh, the display name for GMT+0100
is 格林尼治标准时间+0100 (assuming you're reading this using a font with
Chinese characters, you can see that GMT has been localized). Other locales
localize differently, including some that localize the numeric portion as
well.

At the *very* least, shouldn't this be convId =
zone.getDisplayName(Locale.US)? Or, really, shouldn't this simply use the
time zone ID, that is convId = zone.getId()?

Thanks,

Steve Livengood
Samsung Electronics
Irvine, CA 92612




------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Java TimeZone Conversion

jodastephen
Looks like the specifcation of the methods in the JDK changed between
Java 6 and 7
http://docs.oracle.com/javase/6/docs/api/java/util/TimeZone.html#getDisplayName%28%29
http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#getDisplayName%28%29

But even so, its an odd bit of code.

I've changed it to getID() in git.

thanks
Stephen



2012/2/9 Steve Livengood <[hidden email]>:

> Hi,
>
> I've run into a problem using the Dalvik VM (Android) with the time zone
> conversion functions. Specifically, the implementation of
> DateTimeZone.forTimeZone() fails when the Java timezone is a GMT+/-hh:mm
> format if the current locale is not a western locale. You can easily see the
> failure by using this code snippet:
>
>    Locale.setDefault(Locale.CHINA);
>    TimeZone sTimeZone = java.util.TimeZone.getTimeZone("GMT-01:00");
>    System.out.println(Locale.getDefault());
>    System.out.println(sTimeZone.getDisplayName());
>    DateTimeZone dtz = DateTimeZone.forTimeZone(sTimeZone);
>    System.out.println(dtz.getID());
>
> The invocation of DateTimeZone.forTimeZone() will throw an
> IllegalArgumentException when using a VM that localizes time zones for
> display purposes (specifically, when using the Dalvik VM).
>
> The code in question is:
>
>  public static DateTimeZone forTimeZone(TimeZone zone) {
>
>    .... other code here that converts named time zones ....
>
>    // Support GMT+/-hh:mm formats
>    if (convId == null) {
>        convId = zone.getDisplayName();
>        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
>            convId = convId.substring(3);
>            int offset = parseOffset(convId);
>            if (offset == 0L) {
>                return DateTimeZone.UTC;
>            } else {
>                convId = printOffset(offset);
>                return fixedOffsetZone(convId, offset);
>            }
>        }
>    }
>    throw new IllegalArgumentException("The datetime zone id '" + id + "' is
> not recognised");
>
> Specifically, the line, convId = zone.getDisplayName(). This returns the
> locale-specific display name for the time zone. In the Sun VM, when the time
> zone is a custom GMT+nn:nn or GMT-nn:nn, the display name is also of this
> same form for every locale. However, in the Dalvik VM, it is properly
> localized. For example, when the local is zh, the display name for GMT+0100
> is 格林尼治标准时间+0100 (assuming you're reading this using a font with
> Chinese characters, you can see that GMT has been localized). Other locales
> localize differently, including some that localize the numeric portion as
> well.
>
> At the *very* least, shouldn't this be convId =
> zone.getDisplayName(Locale.US)? Or, really, shouldn't this simply use the
> time zone ID, that is convId = zone.getId()?
>
> Thanks,
>
> Steve Livengood
> Samsung Electronics
> Irvine, CA 92612
>
>
>
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...