How to get the abbreviation / Country code of mobile timezone in android?

百般思念 提交于 2019-12-01 05:50:23
Meno Hochschild

About terminology:

We call strings like "America/Denver" etc. (in format region/city) timezone identifiers, not names. These ids are defined in IANA-TZDB. Timezone names and abbreviations are a totally different concept. Those data usually have their origins in CLDR-files (from Unicode consortium, for example here) and correspond to what most people expect IN THEIR LOCALES. These names are highly localized. In general, there is no international standard, neither for identifiers nor for names. Latter ones are just following local customs.

Mapping:

A mapping from timezone identifiers to country codes and vice versa can be found in IANA-TZDB. You can then quickly see that there is no unique mapping as you expect. Examples:

Europe/Zurich can be valid in Switzerland, Germany(sic!) and Liechtenstein.

Or trivially:

US has more than one timezone (America/New_York, America/Chicago, etc.)

Your hope to get unique timezone abbreviations using the expression

Calendar.getInstance().getTimeZone().getDisplayName(false, TimeZone.SHORT);

is not realistic. The method to get tz abbreviations you describe is correct. However, as you yourself have noticed in your other SO-post the results differ from one Android device to another. The main reason is that different devices (dalvik vms) often have different data for timezone names (and the OpenJDK-Oracle-VMs, too).

A unique mapping from timezone names and abbreviations to country codes is usually not possible. Counter examples: "IST" can stand for "India Standard Time" or for Israel time zone. Or: "BST" is used in "Europe/London" (during summer time) or in "Pacific/Bougainville" (all the year).

About daylight saving:

Identifiers are NOT sensitive for daylight saving. The related data can be daylight saving or not, dependent on the associated time. Timezone names and abbreviations are often (not always) sensitive, compare for example "PST" or "PDT" in US.

Summary and advice:

Do not use timezone names or abbreviations for anything other than display purposes. You should not use these localized data for any conclusions or storage purposes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!