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

我有一个要求,我必须将时区从UTC转换为特定的时区,反之亦然,考虑到日光节省.我正在使用

java.util.TimeZone类.现在,问题是时区有几百个ID无法显示给用户.

作为一项工作,我们现在决定首先列出国家名单,并列出所选国家的时区.我无法获得ISO country code的TimeZone.

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

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));

我需要找出在上面的代码中用于给定国家/地区ISO代码的时区ID.

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

码:

public class TimeZones {

private static final String TIMEZONE_ID_PREFIXES =

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

private List timeZones = null;

public List getTimeZones() {

if (timeZones == null) {

initTimeZones();

}

return timeZones;

}

private void initTimeZones() {

timeZones = new ArrayList();

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() {

public int compare(final TimeZone a, final TimeZone b) {

return a.getID().compareTo(b.getID());

}

});

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值