如何在Java 8时间库中获取3个字符的时区显示名称?

huangapple go评论59阅读模式
英文:

How to get 3-character time zone display names in Java 8 time library?

问题

当夏令时(DST)激活时,我可以使用java.util.TimeZone打印出"EDT",如下所示:

TimeZone.getTimeZone("America/New_York").getDisplayName(true, TimeZone.SHORT) -> 返回 "EDT"

我想要在Java 8的新时间库中获得相同的3个字符。我尝试了以下方法,但只得到了"ET":

ZonedDateTime.now().getZone().getDisplayName(TextStyle.SHORT, Locale.US) -> 返回 "ET"

是否有任何方法可以根据夏令时(DST)状态在新库中获取3字符的显示名称(如EST、EDT、PST、PDT等)?

英文:

When DST is active, I can print EDT with java.util.TimeZone as follows:

TimeZone.getTimeZone("America/New_York").getDisplayName(true, TimeZone.SHORT) -> Returns "EDT"

I want to get the same 3-character with Java 8's new time library. I try this but I only get ET:

ZonedDateTime.now().getZone().getDisplayName(TextStyle.SHORT, Locale.US) -> Returns "ET"

Is there any way to get 3-character display names (EST, EDT, PST, PDT, etc.) in the new library, depending on the DST status?

答案1

得分: 3

你可以这样使用 DateTimeFormatter

ZonedDateTime.now().format(DateTimeFormatter.ofPattern("z"))

在你的 ZonedDateTime 中的 ZoneId 实际上没有这个信息。

英文:

You can get using DateTimeFormatter this way

ZonedDateTime.now().format(DateTimeFormatter.ofPattern("z"))

ZoneId in your ZonedDateTime doesn't have that information accually .

huangapple
  • 本文由 发表于 2020年8月2日 02:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63208929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定