java 如何获取指定 ISO 国家/地区代码的时区 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11155929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:00:12  来源:igfitidea点击:

How to get TimeZone id for specified ISO country code?

javatimezone

提问by Mayank

I have a requirement where I have to convert timezone from UTC to a specific timezone and vice-versa taking into account day light saving. I am using java.util.TimeZoneclass for it. Now, issue is that there are several hundred Ids for timezone which cannot be displayed to user.

我有一个要求,考虑到夏令时,我必须将时区从 UTC 转换为特定时区,反之亦然。我正在java.util.TimeZone为它使用类。现在,问题是时区有数百个 Id 无法向用户显示。

As a work around now we have decided to have country list first and list time-zones for country selected. I am not able to get TimeZonefor an ISO country code.

作为现在的解决方法,我们决定首先列出国家/地区列表并列出所选国家/地区的时区。我不能够得到TimeZone一个ISO国家代码

Here is code which I am currently using to convert timezones,

这是我目前用来转换时区的代码,

Timestamp convertedTime = null;
try{
System.out.println("timezone: "+timeZone +", timestamp: "+timeStamp);
Locale locale = Locale.ENGLISH;
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
        DateFormat formatter = DateFormat.getDateTimeInstance(
                    DateFormat.DEFAULT,
                    DateFormat.DEFAULT,
                    locale);
        formatter.setTimeZone(destTimeZone);
        Date date = new Date(timeStamp.getTime());
        System.out.println(formatter.format(date));
        convertedTime = new Timestamp(date.getTime());
        /*long sixMonths = 150L * 24 * 3600 * 1000;
        Date inSixMonths = new Date(timeStamp.getTime() + sixMonths);
        System.out.println("After 6 months: "+formatter.format(inSixMonths));

I need to find out timezone Id to be used in above code for given country ISO code.

我需要找出要在给定国家/地区 ISO 代码的上述代码中使用的时区 ID。



Update: tried many things and below code gets me to list of timezones with 148 entries (which is still large). Can any one please help me to shorten it. Or, suggest some other way to either have a shorten list of timezones or get timezones for a country,

更新:尝试了很多东西,下面的代码让我得到了包含 148 个条目(仍然很大)的时区列表。任何人都可以帮我缩短它。或者,建议一些其他方法来缩短时区列表或获取某个国家/地区的时区,

Code:

代码:

public class TimeZones {

private static final String TIMEZONE_ID_PREFIXES =
  "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";

private List<TimeZone> timeZones = null;

public List<TimeZone> getTimeZones() {
  if (timeZones == null) {
     initTimeZones();
  }

  return timeZones;
}

private void initTimeZones() {
  timeZones = new ArrayList<TimeZone>();
  final String[] timeZoneIds = TimeZone.getAvailableIDs();
  for (final String id : timeZoneIds) {
     if (id.matches(TIMEZONE_ID_PREFIXES)) {
        timeZones.add(TimeZone.getTimeZone(id));
     }
  }
  Collections.sort(timeZones, new Comparator<TimeZone>() {
     public int compare(final TimeZone a, final TimeZone b) {
        return a.getID().compareTo(b.getID());
     }
  });
}

采纳答案by Mayank

Was able to get things working. I have created own database table with all time-zones as appearing in windows OS and their corresponding TimeZone IDs. Conversion is done using java.util.TimeZone class.

能够让事情发挥作用。我创建了自己的数据库表,其中包含出现在 Windows 操作系统中的所有时区及其相应的时区 ID。转换是使用 java.util.TimeZone 类完成的。

Thanks Namal and Frank for your inputs.

感谢 Namal 和 Frank 的投入。

回答by namalfernandolk

I think ICU4J packagewill help you.

我认为ICU4J 包会帮助你。

回答by Franz Ebner

You can shorten your list with hasSameRules()... this should reduce you selection to about 50:

您可以使用 hasSameRules() 缩短您的列表……这应该可以将您的选择减少到大约 50 个:

iterate through -> file equal time zones -> choose the most recognizables

迭代 -> 文件相等时区 -> 选择最容易识别的

The country- list has to have about 200 entries with a whole lot of uninteresting ones such as Gibraltar or St Martin... don't like that idea

国家/地区列表必须有大约 200 个条目,其中包含大量无趣的条目,例如直布罗陀或圣马丁……不喜欢这个想法