Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out time zone abbreviations when using offset hours in Joda Time?

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?

like image 470
emmby Avatar asked Nov 11 '09 22:11

emmby


1 Answers

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.

like image 169
candiru Avatar answered Nov 12 '22 07:11

candiru