ISO_DATE_TIME.format() 到具有可选偏移量的 LocalDateTime

我正在尝试将 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?. 我想避免一些字符串解析(检查字符串是否包含“+”/“-”)。


智慧大石
浏览 104回答 2
2回答

慕森王

您可以构建一个带有可选偏移量元素的解析器,并使用 TemporalAccessor.isSupported 检查偏移量是否存在。    DateTimeFormatter parser = new DateTimeFormatterBuilder()        .parseCaseInsensitive()        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)        .optionalStart()        .appendOffsetId()        .optionalEnd()        .toFormatter();    TemporalAccessor accessor = parser.parse(isoDateTime);    if (accessor.isSupported(ChronoField.OFFSET_SECONDS)) {        var zoned = ZonedDateTime.from(accessor);        return zoned.withZoneSameInstant(ZoneId.of(timezone)).toLocalDateTime();    }    return LocalDateTime.from(accessor);

白衣染霜花

您可以在子句中处理解析异常catch并尝试不同的解析器。例如像这样:String timezone = "Pacific/Apia"String isoDateTime = "2011-12-03T10:15:30+03:00";    try{    var zoned = ZonedDateTime.from(ISO_DATE_TIME_FORMATTER.parse(isoDateTime));    return zoned.withZoneSameInstant(ZoneId.of(timeZone)).toLocalDateTime();} catch (DateTimeException e) {    //no time zone information -> parse as LocalDate    return LocalDateTime.parse(isoDateTime);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java