Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 746
M Vignesh Avatar asked Mar 31 '15 06:03

M Vignesh


People also ask

How do you use time zone abbreviations?

If you need to indicate that a time is in a certain time zone, the simplest way to do it is to put the time zone abbreviation after the time: for Eastern Standard Time, write “4:30 p.m. EST.”

How do I get mobile time zone?

getDefault() on android 4.2. TimeZone. getDefault() will return the timezone selected in settings -> date & time.


1 Answers

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.

like image 150
Meno Hochschild Avatar answered Nov 03 '22 00:11

Meno Hochschild