时区之间的转换导致意外行为

时间:2019-03-20 09:33:03

标签: java scala datetime

我正在Scala中做一个简单的练习,试图将莫斯科时间转换为伦敦和纽约

import java.text.SimpleDateFormat
import java.util.TimeZone
inputFormat.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"))
val inMoscow = inputFormat.parse("2020-03-19 12:44 Z")
inMoscow: java.util.Date = Thu Mar 19 12:44:00 MSK 2020 

让我们尝试将时间表示转换为伦敦:

val outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm Z")
outputFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"))
val outLondon = outputFormat.format(inMoscow)
outputFormat: SimpleDateFormat = java.text.SimpleDateFormat@c07fb1f4
outLondon: String = "2020-03-19 09:44 +0000"

一切正常。但是,当我尝试转换为纽约时间时,会得到意外的结果:

val outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm")
outputFormat.setTimeZone(TimeZone.getTimeZone("Americas/New_York"))
val outNY = outputFormat.format(inMoscow)
outputFormat: SimpleDateFormat = java.text.SimpleDateFormat@ba23d43a
outNY: String = "2020-03-19 09:44"

与伦敦相同。我错过了什么? 预先感谢!

1 个答案:

答案 0 :(得分:5)

您确定时区为Americas/New_York。我认为是America/New_York,即在美国没有。

相关问题