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

1

9

How to get the Time Zone Abbreviation like IST (India Standard Time), ET (Eastern Time) and all.

I have used the below code to get the time zone information in android,

Option 1:

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

I chosen different time zone and the output as follows,

GMT+13:00

GMT+00:00

But when settings some of the time zone it gives me the proper abbreviation code as output which is below,

EST

And also I tried the below code which gives the full description of timezone.

Option 2:

String mobileTimeZone = Calendar.getInstance().getTimeZone().getID();

Getting the below output which is nothing but zone name:

Pacific/Midway

Pacific/Honolulu

America/Montevideo

Is there any list of id or table having all the timezone for me to map it.

And also need to consider the day light savings.

Help me to get the abbreviation of Time Zone from android mobile.

Inexact answered 31/3, 2015 at 6:35 Comment(6)
Have you visited thisBlown
Yes referred. That also not helpful. Is old post says the method is deprecated. not getting the proper solution.Inexact
Your question is duplicated with How to get TimeZone from android mobile?Azaleeazan
Not duplicated. Already referred that question. My question is to get the country code. Not zone name or time zone object.Inexact
Try this Q&A How to get Country (or its ISO code)?Azaleeazan
No that post is to get the country code by using sim card[Telephony Manager]. First understand my question. When ever the user changes the timezone i need to get the country code of that time zone. Not getting the country name based on sim. I hope @Blown understands clearly.Inexact
H
12

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.

Heartstricken answered 31/3, 2015 at 14:52 Comment(2)
@ Meno... Thanks for your full descriptive answers... Is much clear and neat. I thought of just displaying the user time zone in the UI by doing mapping by own. As you suggested we don't have to do the mapping by our-self because IST itself having there different meaning. So we removed this feature of showing timezone to user. Once again thanks for your support.Inexact
Well, I would not say that localized tz names are bad. Many users like to see it (in their language and locale). But I would also advocate to present the concrete numerical tz offset relative to UTC, too to help users to interprete the presented timezone and associated timestamp. Maybe the identifiers as additional info for users, but those identifiers are rather technical and still not related to a unique clear offset information.Heartstricken

© 2022 - 2024 — McMap. All rights reserved.