8

我正在使用 Joda Time,并且正在传递使用DateTimeZone.forOffsetHours(). 我想使用标准时区首字母缩写词(例如“PST”、“EST”等)打印这些时区。

但是,每当我打印使用这些时区的 DateTimes 时,我都会得到时区的“hh:mm”表示,而不是名称的首字母缩写词。

这是一个例子:

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) )) );
    }
}

有没有办法在最后一个示例中使用 Joda Time 打印出适当的时区首字母缩写词?

4

1 回答 1

6

不,这是不可能的,但这不是 joda-time 问题,这是因为时区的工作方式。

偏移量(例如 UTC-8)不能确定位置,因此也不能确定取决于位置的首字母缩写词。正如您在此处看到的,多个时区都有 UTC-8。

第一个示例有效,因为您的默认时区是 PST。第二个之所以有效,是因为您按其名称询问时区(包括所有夏令时等)。在第三个中,您将获得一个没有与之关联的名称的固定偏移时区。

于 2009-11-12T06:08:30.960 回答