views:

252

answers:

1

I'm using Joda Time, and I'm being passed DateTimeZones that are created using DateTimeZone.forOffsetHours(). I'd like to print these timezones using standard timezone acronyms such as "PST", "EST", etc.

However, whenever I print DateTimes that use these timezones, I get an "hh:mm" representation of the timezone instead of the name acronym.

Here's an example:

public class tmp {
    public static void main( String args[] ) {
        // "PST"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime() ) );

        // "PST"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime( DateTimeZone.forTimeZone( TimeZone.getTimeZone("PST")) )) );

        // "-08:00"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime( DateTimeZone.forOffsetHours(-8) )) );
    }
}

Is there a way to print out the appropriate timezone acronym in the last example using Joda Time?

+1  A: 

No, it is not possible, but it's not a joda-time issue, it's because of how timezones work.

The offset (e.g. UTC-8) doesn't determine the location, so is also doesn't determine the acronym that depends on the location. As you can see here, there is UTC-8 in multiple timezones.

The first example works because your default timezone is PST. The second one works because you ask for a timezone by its name (with all the daylight saving stuff, etc.). In the third one you get a fixed-offset timezone that has no name associated with it.

candiru
I see. I was thinking it might be something like that. Thanks for the info, that makes sense.
Mike