TimeZones in Java

后端 未结 9 1323
执念已碎
执念已碎 2020-12-03 07:19

I am allowing users on my web app to schedule events based on time zones of their choice.

I want to present a good list of time zones to the end user and then conver

9条回答
  •  离开以前
    2020-12-03 07:28

    Just to complement the answer by tbruyelle I added a few more countries (e.g. Canada), removed the "/" portion of the filter and provided a means to sort the list.

    public static void main(String[] args)
    {
        List simplifiedTimezoneList = getTimezoneIdList();
    
        for (String tz : simplifiedTimezoneList)
            System.out.println(tz);
    }
    
    public static List getTimezoneIdList()
    {
        String[] temp = TimeZone.getAvailableIDs();
        List timezoneList = new ArrayList();
        List simplifiedTimezoneList = new ArrayList();
        for (String tz : temp)
        {
            timezoneList.add(tz);
        }
        Collections.sort(timezoneList);
        String filterList = "Canada|Mexico|Chile|Cuba|Brazil|Japan|Turkey|Mideast|Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific";
        Pattern p = Pattern.compile("^(" + filterList + ").*");
        for (String tz : timezoneList)
        {
            Matcher m = p.matcher(tz);
            if (m.find())
            {
                simplifiedTimezoneList.add(tz);
            }
        }
        return simplifiedTimezoneList;
    }
    

提交回复
热议问题