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

ISO_DATE_TIME.format() to LocalDateTime with optional offset

我正在尝试将 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: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed

我知道为什么会出现此异常,问题是如何将包括偏移量在内的两个日期都转换为 LocalDateTime?。我想避免一些字符串解析(检查字符串是否包含“+”/“-”)。

您可以在 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);
}

您可以构建一个带有可选偏移元素的解析器,并使用 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);