开发者

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 acrony开发者_如何学编程m in the last example using Joda Time?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜