Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get available timezones (android:settings)

I want to get TimeZones, that I can see in settings: Date and Time -->

Select time zone

Yes, I know, that in java/android can get all timezones by TimeZone.getAvailableIDs(), but I want get only timezones, that exists on

settings screen.

like image 876
Yura Shinkarev Avatar asked Dec 27 '12 17:12

Yura Shinkarev


2 Answers

As you noticed, it is possible to get a list of all the TimeZones available on your device by invoking TimeZone.getAvailableIDs(), however, this usually returns you a list of 500+ TimeZones with a displayName that's not always user friendly (like a GMT+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 and text in a List of Timezone Map.

public static SimpleAdapter constructTimezoneAdapter(Context context,
            boolean sortedByName, int layoutId) {
        final String[] from = new String[] {ZoneGetter.KEY_DISPLAYNAME, ZoneGetter.KEY_GMT};
        final int[] to = new int[] {android.R.id.text1, android.R.id.text2};

        final String sortKey = (sortedByName ? ZoneGetter.KEY_DISPLAYNAME : ZoneGetter.KEY_OFFSET);
        final MyComparator comparator = new MyComparator(sortKey);
        final List<Map<String, Object>> sortedList = ZoneGetter.getZonesList(context);
        Collections.sort(sortedList, comparator);
        final SimpleAdapter adapter = new SimpleAdapter(context,
                sortedList,
                layoutId,
                from,
                to);

        return adapter;
    }

Next, for each Timezone Map in that List, they retrieve the associated TimeZone thanks to TimeZone.getTimeZone(String id) and add the associated offset and GMT 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 same displayName as other ones, like: Europe/Amsterdam and Europe/Brussels both refer to European 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

like image 102
Snaker Avatar answered Nov 04 '22 13:11

Snaker


String[]TZ = TimeZone.getAvailableIDs();
        ArrayList<String> TZ1 = new ArrayList<String>();
        for(int i = 0; i < TZ.length; i++) {
            if(!(TZ1.contains(TimeZone.getTimeZone(TZ[i]).getDisplayName()))) {
                TZ1.add(TimeZone.getTimeZone(TZ[i]).getDisplayName());
            }
        } 

I think this one is help you

like image 24
QuokMoon Avatar answered Nov 04 '22 13:11

QuokMoon