我正在尝试将 XMLGregorianCalendarObject 转换为 LocalDateTime 并且我得到了不寻常的结果。我已经尝试过这篇文章和这篇文章中的解决方案。
我在这里做了一些我可能错了的假设:
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”
本地日期时间本地日期:“ 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;
}
结果与第一个代码片段相同。
我认为我的问题出在 withZoneSameInstant() 方法中。奇怪的是,当我将不同的时区代码输入参数时,确实会发生转换。试试“太平洋/奥克兰”。
我究竟做错了什么?