Cách tiếp cận sau đây đã hiệu quả.
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);
}
Cũng có thể có những cách khác và có lẽ tốt hơn mà hiện tại tôi không biết.
Hoặc
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);
}
Đối với Greenwich Mean Time (Etc/GMT+0
, ví dụ), nó sẽ hiển thị, ví dụ +00:00
thay vì hiển thị GMT+00:00
như trong trường hợp đầu tiên.
Nếu tên của nó là không có sẵn cho các địa phương, sau đó phương pháp này (public final String getName(long instant)
) trả một chuỗi theo định dạng [+ -] hh: mm.
Một thích hợp Locale
cũng có thể được sử dụng, nếu cần sử dụng phương pháp quá tải,
public String getName(long instant, Locale locale)
tên ngắn, ví dụ UTC cho Coordinated Universal Time thể được hiển thị như sau.
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);
}
Với một thích hợp Locale
, nếu cần sử dụng phương pháp quá tải,
public String getShortName(long instant, Locale locale)
Cập nhật:
Sử dụng Java Time API trong Java SE 8 mà điều này là đơn giản hơn nữa .
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);
}
Tên hiển thị có nhiều kiểu khác nhau có sẵn trong java.time.format.TextStyle
. Ví dụ: chữ viết tắt có thể được hiển thị bằng cách sử dụng TextStyle.SHORT
.
zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH)
sẽ hiển thị tên dài như "Thời gian Ấn Độ". Tuy nhiên, đây không phải là tên đầy đủ, không giống như Joda Time.
Sau đây sẽ hiển thị tên đầy đủ của tên đã cho như "Giờ chuẩn Ấn Độ" (nếu có).
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("zzzz");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));
Sau đây sẽ hiển thị vùng bù đắp của vùng đã cho như GMT+05:30
(lưu ý cách viết hoa của mẫu).
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("ZZZZ");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));
Phần sau đây để hiển thị chữ viết tắt.
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("zzz");
String longName = pattern.format(ZonedDateTime.now(ZoneId.of(zoneId)));
Capital ZZZ
cho khu bù đắp như +0530
, +0000
.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html