How I can get available timezones (android:settings)

后端 未结 4 1248
余生分开走
余生分开走 2021-01-18 06:16

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

相关标签:
4条回答
  • 2021-01-18 06:50

    Once you've called TimeZone.getAvailableIDs();, you can use TimeZone.getTimeZone(id) to get a TimeZone object. With this object, you can get the name (using .getDisplayName()), or the offset (using .getRawOffset(), which returns the millisecond offset).

    String[] timezones = TimeZone.getAvailableIDs();
    for (String tzId : timezones) {
        TimeZone tz = TimeZone.getTimeZone(tzId);
        String name = tz.getDisplayName();
        int offset = tz.getRawOffset();
        // ...
    }
    
    0 讨论(0)
  • 2021-01-18 07:01

    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

    0 讨论(0)
  • 2021-01-18 07:11

    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

    0 讨论(0)
  • 2021-01-18 07:12
    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

    0 讨论(0)
提交回复
热议问题