从一些生成的代码中,我得到了一个javax.xml.datatype.XMLGregorianCalendar,我希望将它转换为没有任何区域偏移(UTC)的LocalDateTime。
我的当前代码实现了它,但我认为必须能够以一种更优雅(更短)的方式来缓存同样的缓存。
public static LocalDateTime xmlGregorianCalendar2LocalDateTime(XMLGregorianCalendar xgc) {
// fix the time to UTC:
final int offsetSeconds = xgc.toGregorianCalendar().toZonedDateTime().getOffset().getTotalSeconds();
final LocalDateTime localDateTime = xgc.toGregorianCalendar().toZonedDateTime().toLocalDateTime(); // this simply ignores the timeZone
return localDateTime.minusSeconds(offsetSeconds); // ajust according to the time-zone offet
}发布于 2019-03-07 13:37:34
类似于:
xgc.toGregorianCalendar().toZonedDateTime().toLocalDateTime() ?如果您不想窃取区域信息,而是在UTC获取本地时间:
ZonedDateTime utcZoned = xgc.toGregorianCalendar().toZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"));
LocalDateTime ldt = utcZoned.toLocalDateTime();这个答案来自编写了java.time规范并实现它们的人--顺便说一句:https://stackoverflow.com/questions/29767084/convert-between-localdate-and-xmlgregoriancalendar
https://codereview.stackexchange.com/questions/214711
复制相似问题