I want to get TimeZones, that I can see in settings: Date and Time -->
Yes, I know, that in java/android can get all timezones by TimeZone.getAvailableIDs()
, but I want get only timezones, that exists on
.
I want to get TimeZones, that I can see in settings: Date and Time -->
Yes, I know, that in java/android can get all timezones by TimeZone.getAvailableIDs()
, but I want get only timezones, that exists on
.
Yes, I already try to select one offset - one timezone. But in this case get another error. I used this timezone on programmatically add event to google calendar (parse ics-file). With some timezone I have issue. For example, I add event with timezone +6 GMT.
getAvailableIDs() return with this offset some first timezone (I do not exactly remember, but timezone was ".../Vostok"). If I try add event with this timezone, then I opened this added event in Google Calendar android application and see, that timezone set not as normal "America/New York" or "Europe/Moscow", but simply "+ 6 GMT".
So I began to look list "standart" timezones. Now I used some another way. I search in google calendar source and find that they do not get timezone in runtime with some proc, but simply set some timezone ids in static array. Now I also used this list. I do not know, how this list changed from android version. But worked correct on 4.x and 2.x
As you noticed, it is possible to get a list of all the
TimeZone
s available on your device by invokingTimeZone.getAvailableIDs()
, however, this usually returns you a list of 500+TimeZone
s with adisplayName
that's not always user friendly (like aGMT+05:00
).So now, if you take a look at Android's timezones list, it is only comprised of about 80 elements, with nicely formatted names and all. By looking at the source code (up to Kitkat), you can see that they actually retrieve their timezones from a xml located in
/res/xml/timezones.xml
. Each row is defined like follow:<timezone id="America/New_York">Eastern Time</timezone>
So what they do, is parse the whole
timezones.xml
file and put the (Olson)id
andtext
in a List of TimezoneMap
.Next, for each Timezone
Map
in that List, they retrieve the associatedTimeZone
thanks toTimeZone.getTimeZone(String id)
and add the associatedoffset
andGMT
to it.So in the end, what they display really, is a list of timezones where they retrieve the IDs from a file:
timezones.xml
.Something that could work if you have one id per timezone, is to just make up a list of Olson ids and show their
displayName
. That might not work if you have more since some Olson ids hold the samedisplayName
as other ones, like:Europe/Amsterdam
andEurope/Brussels
both refer toEuropean Central Time
.Note: This post is mostly focused on Kit Kat and might not apply to lower/higher version. If you wish to get more info for your own version, please refer to: this link
Once you've called
TimeZone.getAvailableIDs();
, you can useTimeZone.getTimeZone(id)
to get aTimeZone
object. With this object, you can get the name (using.getDisplayName()
), or the offset (using.getRawOffset()
, which returns the millisecond offset).