java - 多个java的TimeZone被定向到单个joda的DateTimeZone

标签 java timezone jodatime

我使用 joda 的 DateTimeZone 及其所有功能。

我从第三方获得了 java 标准时区。

然后我将其转换为 DateTimeZone,如下所示:

TimeZone timeZone = TimeZone.getTimeZone(3rd_party_timezone_string_id);
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);

我注意到多个时区映射到同一个 DateTimeZone。例如:

“澳大利亚/阿德莱德”-->“澳大利亚/阿德莱德”

“澳大利亚/南部”-->“澳大利亚/阿德莱德”

我想知道,当我有了 DateTimeZone 时,如何获取映射到它的 TimeZone 列表?

最佳答案

如果您从 IANA 获取时区数据和代码(截至 2014 年 11 月 11 日的 tzcodetzdata),在 tzdata 中您可以找到向后 文件,提供时区当前名称与其旧名称之间的链接。

例如,在该文件中您可以找到您提到的文件:

(...)
Link    Australia/Adelaide  Australia/South
(...)

如果您想拥有这样的 ID“映射”,您可以迭代可用的 ID,并使用 DateTimeZone.forID() 获取当前名称。

groovy 中的这个小脚本可以满足您的需求(您可以轻松地将其移植到 java):

@Grapes(
    @Grab(group='joda-time', module='joda-time', version='2.5')
)

import org.joda.time.*
import org.joda.time.format.*

Map<String, List<String>> equivalentZones = new HashMap<String, List<String>>()

DateTimeZone.getAvailableIDs().each { id ->
    DateTimeZone dtz = DateTimeZone.forID(id)
    zonesForID = equivalentZones.get(dtz.ID, [])
    if (id != dtz.ID) {
        zonesForID << id        
    }
    equivalentZones.put(dtz.ID, zonesForID)
}

equivalentZones.each { k,v ->
    println "$k -> $v"
}

它产生:

(...)
Africa/Maputo -> [Africa/Blantyre, Africa/Bujumbura, Africa/Gaborone, Africa/Harare, Africa/Kigali, Africa/Lubumbashi, Africa/Lusaka]
(...)
Asia/Shanghai -> [Asia/Chongqing, Asia/Chungking, Asia/Harbin, PRC]
(...)
Australia/Adelaide -> [Australia/South]
(...)
Europe/London -> [Europe/Belfast, Europe/Guernsey, Europe/Isle_of_Man, Europe/Jersey, GB, GB-Eire]
(...)

关于java - 多个java的TimeZone被定向到单个joda的DateTimeZone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904305/

相关文章:

java - android: TimeZone.getDefault() 返回 EST,而不是 EDT

java - 将 joda-time-android 与混淆器一起使用

java - 与时区无关的日期/时间

java - android.support.v7.widget.GridLayoutManager$LayoutParams 无法转换为 android.widget.LinearLayout$LayoutParams

java - 如何在 IntelliJ 中正确配置 Eclipse 项目?

c# - 将 future 的本地日期时间转换为 UTC

java - 使用 JodaTime 将本地时间转换为特定国家/地区时间

java - 抓取网页编码问题-以字节为单位的负值

Java 程序不会循环 while 循环 - 直接进入 if 语句

php - 从纬度和经度获取 PHP 时区名称?