使用 JodaTime / scala 使用 Timezone 转换时间

Converting time using Timezone with JodaTime / scala

我正在尝试利用一些 JodaTime 方法来改进我正在处理的基本功能。

前提是我需要根据给定的时区将时间从一个转换为另一个。

我有一个儿子的时代清单:

  "name": "A",
  "timezone": "UTC",
  "jodaDate": "2016-01-12T14:33:37.533"
},
{
  "name": "C",
  "timezone": "UTC",
  "jodaDate": "2016-01-21T10:33:37.533"
}

然后我 运行 通过一个函数获取代表上述各项的对象和一个 'to' 用于将其转换为另一个时区。

目前我只关注 UTC 到 ECT

Coordinated Universal Time is 5 hours ahead of Ecuador Time
12:33 Sunday, Coordinated Universal Time (UTC) is
07:33 Sunday, Ecuador Time (ECT)

我传入"ECT"作为参数,然后进行转换。问题是 ECT 不知何故变成了 'CET'。我很困惑。我确定这很简单,但我就是找不到错误。

方法(包括精彩的printlns...)

val currentTimezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezoneDetails.timezone))
println("Current Timezone from object: " + currentTimezone)

println("arg for converting to: "+to)
val s = DateTimeZone.forTimeZone(TimeZone.getTimeZone(to))
println("arg when converted to timezone: " + s)

val y = timezoneDetails.jodaDate.withZone(s)
println("Was: " + timezoneDetails.jodaDate)
println("Now: " +y)
y

这将产生:

    Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2016-01-12T14:33:37.533Z
Now: 2016-01-12T15:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2016-01-21T10:33:37.533Z
Now: 2016-01-21T11:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2015-12-27T10:33:37.533Z
Now: 2015-12-27T11:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2015-11-23T14:33:37.533Z
Now: 2015-11-23T15:33:37.533+01:00

我认为问题出在 val s = DateTimeZone.forTimeZone(TimeZone.getTimeZone(to)) 但我不明白为什么

有什么想法吗?提前致谢!

问题出在 TimeZone.getTimeZone(name):

public static void main(String[] args) {
    TimeZone tz = TimeZone.getTimeZone("ECT");
    System.out.println("Zone: " + tz.getDisplayName(Locale.US));
    }

会打印出Zone: Central European Time

文档说:

三字母时区 ID

为兼容JDK 1.1.x,一些其他三字母时区ID(如"PST"、"CTT"、"AST")也支持。但是,它们的使用已被弃用,因为相同的缩写经常用于多个时区(例如,"CST" 可能是 U.S。"Central Standard Time" 和 "China Standard Time"),并且 Java 平台只能识别其中一个。