TimeZones in Java

孤者浪人 提交于 2019-12-17 10:59:07

问题


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 convert it easily to java.util.TimeZone object at the server end.

String[] TimeZone.getAvailableIds() is something I could use, but the issue is that it prints about 585 time zone ids.

What is the best way to present to the user a brief list of time zones (like a Windows box would for time zone settings) and easily convert to TimeZone object at server end using its id?


回答1:


The list of timezones is very application and locale specific. Only you know what zones are most applicable to your users. We actually have different lists for different regions.

Here is our list for US users for your reference,

    "Pacific/Midway",
    "US/Hawaii",
    "US/Alaska",
    "US/Pacific",
    "America/Tijuana",
    "US/Arizona",
    "America/Chihuahua",
    "US/Mountain",
    "America/Guatemala",
    "US/Central",
    "America/Mexico_City",
    "Canada/Saskatchewan",
    "America/Bogota",
    "US/Eastern",
    "US/East-Indiana",
    "Canada/Eastern",
    "America/Caracas",
    "America/Manaus",
    "America/Santiago",
    "Canada/Newfoundland",
    "Brazil/East",
    "America/Buenos_Aires",
    "America/Godthab",
    "America/Montevideo",
    "Atlantic/South_Georgia",
    "Atlantic/Azores",
    "Atlantic/Cape_Verde",
    "Africa/Casablanca",
    "Europe/London",
    "Europe/Berlin",
    "Europe/Belgrade",
    "Europe/Brussels",
    "Europe/Warsaw",
    "Africa/Algiers",
    "Asia/Amman",
    "Europe/Athens",
    "Asia/Beirut",
    "Africa/Cairo",
    "Africa/Harare",
    "Europe/Helsinki",
    "Asia/Jerusalem",
    "Europe/Minsk",
    "Africa/Windhoek",
    "Asia/Baghdad",
    "Asia/Kuwait",
    "Europe/Moscow",
    "Africa/Nairobi",
    "Asia/Tbilisi",
    "Asia/Tehran",
    "Asia/Muscat",
    "Asia/Baku",
    "Asia/Yerevan",
    "Asia/Kabul",
    "Asia/Yekaterinburg",
    "Asia/Karachi",
    "Asia/Calcutta",
    "Asia/Colombo",
    "Asia/Katmandu",
    "Asia/Novosibirsk",
    "Asia/Dhaka",
    "Asia/Rangoon",
    "Asia/Bangkok",
    "Asia/Krasnoyarsk",
    "Asia/Hong_Kong",
    "Asia/Irkutsk",
    "Asia/Kuala_Lumpur",
    "Australia/Perth",
    "Asia/Taipei",
    "Asia/Tokyo",
    "Asia/Seoul",
    "Asia/Yakutsk",
    "Australia/Adelaide",
    "Australia/Darwin",
    "Australia/Brisbane",
    "Australia/Sydney",
    "Pacific/Guam",
    "Australia/Hobart",
    "Asia/Vladivostok",
    "Asia/Magadan",
    "Pacific/Auckland",
    "Pacific/Fiji",
    "Pacific/Tongatapu",



回答2:


I've just written a small Java utility that provides a list of Windows time zones (the zones in the time zone selection dialog in Windows), and their associated Java TimeZone objects. See https://github.com/nfergu/Java-Time-Zone-List

This is based on the CLDR mappings at http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml




回答3:


You can reduce the list with TZ IDs which match only the following regexp

^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*



回答4:


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<String> simplifiedTimezoneList = getTimezoneIdList();

    for (String tz : simplifiedTimezoneList)
        System.out.println(tz);
}

public static List<String> getTimezoneIdList()
{
    String[] temp = TimeZone.getAvailableIDs();
    List<String> timezoneList = new ArrayList<String>();
    List<String> simplifiedTimezoneList = new ArrayList<String>();
    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;
}



回答5:


If you need the granularity of choosing exactly how the list would look, I would use the best hard-coded list I could find (this is a good example) and ensure it is displayed and converted as precisely as possible.

Just keep in mind that each one of those 585 time zones does have a semantic meaning (such as DST for example) and users might want to choose the best time zone for them. Although I do agree that list can be much shorter.




回答6:


Couldn't you use a list of custom time zone IDs using "GMT +/- Hours" notation (skipping minutes)?

(EDIT: With my first suggestion, daylight saving time transition is not automatic. To address this issue, you could first ask the user to select a GMT offset and then show a (linked) list of time zone IDs for the given offset using:

public static String[] getAvailableIDs(int rawOffset) 

This way, the user would be able choose his time zone in a shorter list (better for user experience) and benefit from daylight savings behavior.)




回答7:


I did this for a company I don't own any of anymore, so can't provide code. The JVM on Windows comes with a file called tzmappings (look in C:\Program Files\Java\jre6\lib or similar) which maps Windows timezones to Java's zoneinfo-based Continent/City form.

Unfortunately, the textual names in tzmappings are terrible, so you need to do a few minutes of tabulation. Open regedit and navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones. Under this is a key for each timezone on the machine; Windows 7 has about 90. Each key has a value called Display which is the textual name you want; look for the key itself in tzmappings to find the Java time zone identifier for each one.




回答8:


With that many, I wouldn't try to shoehorn them into a select box list.... I'd put them on a list in a separate modal dialog (or popup, if you must), let the user scroll through and click the name they want. They would click on a link in the modal dialog, and it would populate a text field with the correct code, and you could then submit that to your server.

Better yet, have them click their location on a map of the world, and use an image map to translate that location to the appropriate time zone.



来源:https://stackoverflow.com/questions/1694885/timezones-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!