【发布时间】:2019-12-24 19:29:12
【问题描述】:
我正在尝试将 ISO 日期时间转换为 LocalDateTime:
String timezone = "Pacific/Apia";
String isoDateTime = "2011-12-03T10:15:30+03:00";
var zoned = ZonedDateTime.from(ISO_DATE_TIME_FORMATTER.parse(isoDateTime));
return zoned.withZoneSameInstant(ZoneId.of(timeZone)).toLocalDateTime();
此代码有效 - 它将其转换为本地日期,包括偏移量。但问题是当我通过没有偏移的日期时:2011-12-03T10:15:30 -
java.time.DateTimeException: 无法从 TemporalAccessor: {} 获取 ZonedDateTime,ISO 解析为 java.time.format.Parsed 类型的 2011-12-03T10:15:30
我知道为什么会出现这个异常,问题是如何将包括偏移量在内的两个日期转换为 LocalDateTime?。我想避免一些字符串解析(检查字符串是否包含'+'/'-')。
【问题讨论】: