簡體   English   中英

從人類可讀的時區字符串中獲取時區ID

[英]Get timezone ID from human-readable timezone string

有沒有辦法將人類可讀的時區字符串(如Eastern Standard Time轉換為時區ID而不進行硬編碼?

如果我得到時區ID,那么我可以相應地設置時區。

您可以使用TimeZone類枚舉時區ID和字符串。 您還可以從字符串值設置TimeZone。

TimeZone類和Locale類可用於在當前或某個指定時區中查找適當的時區名稱。 例如,這段代碼片段

final Locale fr_FR = Locale.FRANCE;
final Locale de_DE = Locale.GERMANY;
for (String s : TimeZone.getAvailableIDs()) {
    final TimeZone tz = TimeZone.getTimeZone(s);
    sb.append(tz.getDisplayName() + "<br>");
    sb.append(tz.getDisplayName(fr_FR) + "<br>");
    sb.append(tz.getDisplayName(de_DE) + "<br>");
    sb.append("<br>");
}

列出了一些歐洲時區的以下名稱:

Eastern European Standard Time
heure normale de l’Europe de l’Est
Osteuropäische Normalzeit

Western European Standard Time
heure normale d’Europe de l’Ouest
Westeuropäische Normalzeit

Central European Standard Time
heure normale de l’Europe centrale
Mitteleuropäische Normalzeit

您應該使用ID開頭,例如America/New_York

問題是,無論是“東部標准時間”和“東部夏令時”將兩個映射到相同的ID。

另外,這些是顯示名稱的英文形式。 法國的用戶可能使用Europe/Paris區域,而在英語中我們將使用顯示名稱“中歐標准時間”或“中歐夏令時”。 但在法語中,他們會使用“heure normale de l'Europe centrale”或“heureavancéed'Europecentrale”的顯示名稱。 Unicode CLDR具有這些翻譯。 我不確定Android使用它們的程度。

此外,當你說“中央標准時間”時 - 這不一定是唯一的。 你可能在談論America/Chicago ,或者你可能在談論America/Costa_Rica 哎呀,你可能在談論Australia/Adelaide ,雖然有些人可能稱之為“澳大利亞中央標准時間”,但這與稱我們的“美國中央標准時間”差不多。

所以...顯示顯示名稱,但存儲ID。 保留ID並使用它 - 不要試圖從顯示名稱猜測它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM