DateTimeFormatter.format 和 Continent/City

DateTimeFormatter.format with Continent/City

在我的程序中,我在一个文本文件中有一个日期和一个日期模式,该模式与 DateTimeFormatter 一起用于 parse/format 日期。

直到最近,我的日期看起来像这样 01-01-18 00.00.00,00000000 +01:00 并且日期模式像这样 dd-MM-yy HH.mm.ss,SSSSSSSSS xxx。在我的程序开始时,我将带有模式的日期解析为 ZonedDateTime,最后我正在格式化另一个 ZonedDateTime 并覆盖文本文件中的日期,它们应该始终采用相同的格式,这与此模式一起使用.

现在想把Europe/Berlin改成Europe/Berlin,这样CEST改成CET就不会计算错了

所以我的新日期看起来像这样 01-01-18 00.00.00,00000000 Europe/Berlin,我的日期模式像这样 dd-MM-yy HH.mm.ss,SSSSSSSSS z。 现在解析仍然正常,但是当我尝试格式化我的 ZonedDateTime 时,输出总是以 CEST 而不是 Europe/Berlin.

结尾

现在我想知道是否有任何方法可以实现这一点(最好没有 hacky 解决方法),以便格式化日期以 Continent/City.

结尾

要将时区输出为"Europe/Berlin",请使用VV模式字母,例如:

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS VV")
println(ZonedDateTime.now().format(formatter))
// 2018-07-05 23:12:06.901 Europe/Paris

它也可以反向工作:

val date = ZonedDateTime.parse("2018-07-05 12:00:01.123 Europe/Paris", formatter)
println(date)
// 2018-07-05T12:00:01.123+02:00[Europe/Paris]