2013-04-12 27 views
9

Id múi giờ của Thời gian Joda có thể được hiển thị đơn giản với đoạn mã sau.Cách liệt kê chênh lệch múi giờ, ID múi giờ và tên dài trong Thời gian Joda/Java 8?

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

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

Nhưng cách hiển thị chênh lệch múi giờ tương ứng, ID múi giờ và tên dài sao cho danh sách có thể trông giống như sau?

(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 

Chúng cần thiết để liệt kê trong hộp thả xuống để lựa chọn.


Đoạn mã sau hiển thị tên nhưng bù đắp nó hiển thị trông có vẻ rung rinh.

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); 
} 

Trong số danh sách dài nó sẽ hiển thị, một vài trong số họ được thể hiện như thế nào,

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

Các bù đắp nên được như trong danh sách this.

Trả lời

16

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