如何获取尊重夏令时的区域名称(例如EDT)

时间:2019-07-18 13:28:33

标签: android threetenbp

我有ZonedDateTime实例,试图像这样将区域作为字符串(例如EST / EDT)获取:

merchantLocalReceiptDateTime.getZone().getDisplayName(TextStyle.SHORT, Locale.getDefault())

对于我的设置,它实际上返回了EST,而实际上我正在等待EDT。请建议如何获取正确反映夏令时的区域字符串。

3 个答案:

答案 0 :(得分:0)

TimeZone tz = TimeZone.getTimeZone(zonedDateTime.getZone());
for (final String id : TimeZone.getAvailableIDs(tz.getRawOffset())) {
  System.out.println("id of timezone is :  " + id);
}

答案 1 :(得分:0)

好的,我不喜欢这种解决方案,但这是我到目前为止提出的唯一解决方案,也是唯一可行的解​​决方案:

因此,我们已预先初始化ZonedDateTime merchantLocalReceiptDateTime(请记住,这是threetenbp)。然后,我们可以这样做(给定时间是否为夏时制?):

boolean isDaylightSaving = merchantLocalReceiptDateTime.getZone()
                .getRules().isDaylightSavings(merchantLocalReceiptDateTime.toInstant());

然后,要获得尊重夏时制的时区的简短表示,我们可以这样做(TimeZone不是threeten的一部分,它是java.util):

TimeZone.getTimeZone(merchantLocalReceiptDateTime.getZone().getId())
                .getDisplayName(isDaylightSaving, TimeZone.SHORT)

对于纽约(假设设备语言为英语/美国),以上代码会在冬季生成EST,而现在会生成EDT。对于没有特定夏令时名称的时区,它可以给出例如“ GMT + 2”,“ GMT + 3”等。如果语言不同,则可能会得到那些“ GMT +-”。

答案 2 :(得分:0)

//SHORT: CEST
DateTimeFormatter.ofPattern("zzz").format(zonedDateTime)

//SHORT: CET 
ZoneId.getDisplayName(TextStyle.SHORT,Locale.ENGLISH)    

//LONG: Central European Summer Time
DateTimeFormatter.ofPattern("zzzz").format(zonedDateTime)

//LONG: Central European Time
ZoneId.getDisplayName(TextStyle.LONG,Locale.ENGLISH)

//Use this for converting CET to CEST and vice versa
TimeZone tz = TimeZone.getTimeZone(timeZone.getZone());
tz.getDisplayName(true, TimeZone.SHORT, Locale.ENGLISH));