【发布时间】:2020-01-08 21:16:50
【问题描述】:
我正在尝试将 XMLGregorianCalendarObject 转换为 LocalDateTime,但我得到了不寻常的结果。 我已经尝试过this post和this post中的解决方案。
我在这里做了一些可能是错误的假设:
1) xmlDate 参数是 UTC
2) 返回值为 PST
private LocalDateTime convertDate(XMLGregorianCalendar xmlDate) {
GregorianCalendar gc = xmlDate.toGregorianCalendar();
ZonedDateTime zdt = gc.toZonedDateTime();
LocalDateTime localDate = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles")).toLocalDateTime();
return localDate;
}
输出与输入完全相同:
XMLGregorianCalendar xmlDate: "2019-09-03T13:22:38.436-07:00"
LocalDateTime localDate: "2019-09-03T13:22:38"
另外,这不起作用(相同的方法,不同的语法):
private LocalDateTime convertDate(XMLGregorianCalendar xmlDate) {
ZonedDateTime utcZoned = xmlDate.toGregorianCalendar().toZonedDateTime().withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
LocalDateTime localDate = utcZoned.toLocalDateTime();
return localDate;
}
结果和第一个代码sn-p一样。
我认为我的问题出在 withZoneSameInstant() 方法的某个地方。 奇怪的是,当我将不同的时区代码输入参数时,会发生转换。试试“太平洋/奥克兰”。
我做错了什么?
【问题讨论】:
-
我猜可能输入已经在
America/LosAngeles中了。如果指定Pacific/Auckland,偏移量有多大?从 UTC 开始,这是您所期望的吗? -
@JimGarrison 感谢您的光临;我其实希望你能看到这个。事实证明,我是在我自己的时区内查询一个对象,而不是从 UTC 查询一个对象。
标签: java time localdate xmlgregoriancalendar