如何在 Joda Time / Java 8 中列出时区偏移量、时区 ID 和长名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15967642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:28:47  来源:igfitidea点击:

How to list the timezone offset, timezone ID, and long name in Joda Time / Java 8?

javatimezonejava-8jodatimejava-time

提问by Tiny

Time zone ids of Joda Time can simply be displayed with the following segment of code.

Joda Time 的时区 id 可以简单地用以下代码段显示。

Set<String> zoneIds = DateTimeZone.getAvailableIDs();

for(String zoneId:zoneIds) {
    System.out.println(zoneId);
}

But how to display the corresponding timezone offset, timezone ID, and long name so that the list could look something like the following?

但是如何显示相应的时区偏移量、时区 ID 和长名称,以便列表看起来像下面这样?

(GMT-10:00) Pacific/Honolulu, Hawaii Standard Time
(GMT-10:00) Pacific/Johnston, Hawaii Standard Time
(GMT-10:00) Pacific/Fakaofo, Tokelau Time
(GMT-10:00) HST, Hawaii Standard Time

They are needed to list in a drop-down-box for selection.

它们需要在下拉框中列出以供选择。



The following snippet shows the names but the offset it displays looks wonky.

以下代码段显示了名称,但它显示的偏移量看起来很不稳定。

Set<String> zoneIds = DateTimeZone.getAvailableIDs();

for (String zoneId : zoneIds) {
    int offset = DateTimeZone.forID(zoneId).getOffset(new DateTime());
    String longName = TimeZone.getTimeZone(zoneId).getDisplayName();

    System.out.println("(" + offset + ") " + zoneId + ", " + longName);
}

Out of the long list it displays, a few of them are shown like,

在它显示的长列表中,其中一些显示为,

(-36000000) Pacific/Honolulu, Hawaii Standard Time
(-36000000) Pacific/Johnston, Hawaii Standard Time
(-36000000) Pacific/Fakaofo, Tokelau Time
(-36000000) HST, Hawaii Standard Time

The offset should be as shown in thislist.

偏移量应如此列表中所示。

回答by Tiny

The following approach worked.

以下方法有效。

import java.util.Set;
import java.util.TimeZone;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

Set<String> zoneIds = DateTimeZone.getAvailableIDs();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("ZZ");

for (String zoneId : zoneIds) {
    String offset = dateTimeFormatter.withZone(DateTimeZone.forID(zoneId)).print(0);
    String longName = TimeZone.getTimeZone(zoneId).getDisplayName();

    System.out.println("(" + offset + ") " + zoneId + ", " + longName);
}

There could also be other and probably better ways that I'm right now unaware of.

也可能有我现在不知道的其他可能更好的方法。



Or

或者

import java.util.Set;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

Set<String> zoneIds = DateTimeZone.getAvailableIDs();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("ZZ");

for (String zoneId : zoneIds) {
    String offset = dateTimeFormatter.withZone(DateTimeZone.forID(zoneId)).print(0);
    String longName = DateTimeZone.forID(zoneId).getName(DateTimeUtils.currentTimeMillis());

    System.out.println("(" + offset + ") " + zoneId + ", " + longName);
}

For Greenwich Mean Time (Etc/GMT+0, for example), it would display, for example +00:00instead of displaying GMT+00:00as in the first case.

对于格林威治标准时间(Etc/GMT+0例如),它将显示,例如,+00:00而不是GMT+00:00在第一种情况下显示。

If the name is not available for the locale, then this method (public final String getName(long instant)) returns a string in the format [+-]hh:mm.

如果该名称对于区域设置不可用,则此方法 ( public final String getName(long instant)) 返回格式为 [+-]hh:mm 的字符串。

An appropriate Localecan also be used, if needed using the overloaded method,

Locale如果需要,也可以使用适当的重载方法,

public String getName(long instant, Locale locale)


Short names, for example UTC for Coordinated Universal Time can be displayed as follows.

短名称,例如协调世界时的 UTC 可以显示如下。

import java.util.Set;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

Set<String> zoneIds = DateTimeZone.getAvailableIDs();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("ZZ");

for (String zoneId : zoneIds) {
    String offset = dateTimeFormatter.withZone(DateTimeZone.forID(zoneId)).print(0);
    String shortName = DateTimeZone.forID(zoneId).getShortName(DateTimeUtils.currentTimeMillis());

    System.out.println("(" + offset + ") " + zoneId + ", " + shortName);
}

With an appropriate Locale, if needed using the overloaded method,

使用适当的Locale,如果需要使用重载方法,

public String getShortName(long instant, Locale locale)


Update :

更新 :

Using the Java Time APIin Java SE 8 in which this is further simplified.

在 Java SE 8 中使用Java Time API,其中进一步简化了这一点。

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.Set;

Set<String> zoneIds = ZoneId.getAvailableZoneIds();

for (String zoneId : zoneIds) {
    ZoneId zone = ZoneId.of(zoneId);
    ZonedDateTime zonedDateTime = ZonedDateTime.now(zone);

    ZoneOffset offset = zonedDateTime.getOffset();
    String longName = zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH);

    System.out.println("(" + offset + ") " + zoneId + ", " + longName);
}

The display name has various styles available in java.time.format.TextStyle. For example, abbreviations can be displayed using TextStyle.SHORT.

显示名称在java.time.format.TextStyle. 例如,可以使用 显示缩写TextStyle.SHORT

zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH)will display long names like "India Time". This is however, not a full name unlike Joda Time.

zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH)将显示像“印度时间”这样的长名称。然而,这不是一个与 Joda Time 不同的全名。

The following will display a full name of the given name like "India Standard Time" (wherever applicable).

以下将显示给定名称的全名,例如“印度标准时间”(如果适用)。

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("zzzz");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));

The following will display a zone-offset of the given zone like GMT+05:30(note the capitalization of the pattern).

下面将显示给定区域的区域偏移,例如GMT+05:30(注意模式的大写)。

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("ZZZZ");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));

The following is for displaying abbreviations.

以下用于显示缩写。

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("zzz");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));

Capital ZZZfor zone-offset like +0530, +0000.

ZZZ区域偏移的资本,如+0530, +0000

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html