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

柔情痞子 提交于 2019-12-19 17:41:32

问题


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:


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.



来源:https://stackoverflow.com/questions/1718484/how-to-print-out-time-zone-abbreviations-when-using-offset-hours-in-joda-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!