为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

智慧大石 2023-06-14 11:14:08
我正在尝试将 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?. 我想避免一些字符串解析(检查字符串是否包含“+”/“-”)。
查看完整描述

2 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

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


查看完整回答
反对 回复 2023-06-14
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

您可以在子句中处理解析异常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);

}


查看完整回答
反对 回复 2023-06-14
  • 2 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信