在Joda Time中使用偏移时间时,如何打印时区缩写? [英] How to print out time zone abbreviations when using offset hours in Joda Time?
问题描述
我正在使用Joda Time,并且正在传递使用DateTimeZone.forOffsetHours()
创建的DateTimeZones.我想使用标准时区首字母缩写(例如"PST","EST"等)来打印这些时区.
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.
但是,每当我打印使用这些时区的DateTime时,我都会得到时区的"hh:mm"表示形式,而不是首字母缩写.
However, whenever I print DateTimes that use these timezones, I get an "hh:mm" representation of the timezone instead of the name acronym.
这是一个例子:
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打印出适当的时区缩写?
Is there a way to print out the appropriate timezone acronym 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.
偏移量(例如UTC-8)不能确定位置,因此也不能确定取决于位置的首字母缩写词.如您在此处所见,在多个时区中都有UTC-8.
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.
第一个示例有效,因为您的默认时区为PST.第二个之所以起作用,是因为您要求使用时区名称(带有所有的夏时制,等等).在第三个中,您会得到一个固定偏移量的时区,该时区没有与之关联的名称.
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.
这篇关于在Joda Time中使用偏移时间时,如何打印时区缩写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!