[英]Get timezone ID from human-readable timezone string
Is there a way to convert a human-readable timezone string like Eastern Standard Time
to a timezone ID without hard-coding? 有没有办法将人类可读的时区字符串(如
Eastern Standard Time
转换为时区ID而不进行硬编码?
If I get the timezone ID then I can set the timezone accordingly. 如果我得到时区ID,那么我可以相应地设置时区。
You can use the TimeZone class to enumerate the timezone IDs and strings. 您可以使用TimeZone类枚举时区ID和字符串。 You can also set a TimeZone from a string value.
您还可以从字符串值设置TimeZone。
The TimeZone class and Locale class can be used to find the appropriate timezone name in the current or some designated timezone. TimeZone类和Locale类可用于在当前或某个指定时区中查找适当的时区名称。 Eg, this code fragment
例如,这段代码片段
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>");
}
lists the following names for some European timezones: 列出了一些欧洲时区的以下名称:
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
You should be using the ID to begin with, such as America/New_York
. 您应该使用ID开头,例如
America/New_York
。
The problem is that both "Eastern Standard Time" and "Eastern Daylight Time" would both map to the same id. 问题是,无论是“东部标准时间”和“东部夏令时”将两个映射到相同的ID。
Also, those are English forms of the display names. 另外,这些是显示名称的英文形式。 A user in France might use the
Europe/Paris
zone, and in English we would use a display name of "Central European Standard Time" or "Central European Summer Time". 法国的用户可能使用
Europe/Paris
区域,而在英语中我们将使用显示名称“中欧标准时间”或“中欧夏令时”。 But in French they would use a display name of "heure normale de l'Europe centrale" or "heure avancée d'Europe centrale". 但在法语中,他们会使用“heure normale de l'Europe centrale”或“heureavancéed'Europecentrale”的显示名称。 The Unicode CLDR has these translations.
Unicode CLDR具有这些翻译。 I am uncertain as to the degree that Android uses them.
我不确定Android使用它们的程度。
Also, when you say something like "Central Standard Time" - that isn't necessarily unique. 此外,当你说“中央标准时间”时 - 这不一定是唯一的。 You might be talking about
America/Chicago
, or you might be talking about America/Costa_Rica
. 你可能在谈论
America/Chicago
,或者你可能在谈论America/Costa_Rica
。 Heck, you might be talking about Australia/Adelaide
, although some might call that "Australian Central Standard Time", but that's about the same as calling ours "American Central Standard Time". 哎呀,你可能在谈论
Australia/Adelaide
,虽然有些人可能称之为“澳大利亚中央标准时间”,但这与称我们的“美国中央标准时间”差不多。
So... Show the display name, but store the ID. 所以...显示显示名称,但存储ID。 Hold on to the ID and use it - don't try to guess it from the display name.
保留ID并使用它 - 不要试图从显示名称猜测它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.