gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:40:30 24 4
gpt4 key购买 nike

我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com