You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin DateTimeParseException疑难:日期时间解析时有时报错

问题

我有一个时间字符串2023-07-17T12:05:11.03831+00:00,尝试用以下Kotlin代码解析:

val formatPattern =
            DateTimeFormatterBuilder()
                    .appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
                    .parseLenient()
                    .parseCaseInsensitive()
                    .appendOffset("+HH:MM", "+00:00")
                    .toFormatter()
val date = LocalDateTime.parse("2023-07-17T12:05:11.03831+00:00", formatPattern)

但解析操作有时成功,有时抛出如下异常:

Exception in thread "main" java.time.format.DateTimeParseException:
Text '2023-07-17T12:05:11.03831+00:00' could not be parsed at index 20

这段代码在配置了如下编译选项的Android Studio中能正常运行:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

使用Kotlin 1.8.20版本,但在部分设备及Kotlin官方在线编译器中无法运行,却能在另一在线编译器中正常执行,请问可能的问题原因是什么?

原因分析与解决方案

核心问题:小数秒位数不匹配+时间类型误用

  1. 小数秒位数不一致:你的时间字符串里的小数秒是5位(03831),但格式化模式写的是SSSSSS(要求6位)。parseLenient()的宽松解析逻辑在不同Java/Kotlin运行时环境下实现有差异:
    • Android的ART虚拟机对宽松解析的容忍度更高,会自动补零适配6位,所以解析成功;
    • 严格遵循规范的环境(比如Kotlin官方在线编译器)会因为位数不匹配直接抛出异常。
  2. 时间类型错误:你用LocalDateTime解析带时区偏移的字符串本身就不合理——LocalDateTime是不带时区/偏移信息的类型,正确的做法是用OffsetDateTime处理带偏移的时间。

修复后的代码

方案一:精准匹配5位小数秒

val formatPattern = DateTimeFormatterBuilder()
    .appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSS") // 改为5位小数秒匹配
    .parseLenient()
    .parseCaseInsensitive()
    .appendOffset("+HH:MM", "+00:00")
    .toFormatter()
// 使用OffsetDateTime解析带时区偏移的时间
val date = OffsetDateTime.parse("2023-07-17T12:05:11.03831+00:00", formatPattern)

方案二:兼容1-6位小数秒(更灵活)

用方括号标记小数秒部分为可选,同时支持1到6位的小数秒:

val formatPattern = DateTimeFormatterBuilder()
    .appendPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]") // 可选的1-6位小数秒
    .parseLenient()
    .parseCaseInsensitive()
    .appendOffset("+HH:MM", "+00:00")
    .toFormatter()
val date = OffsetDateTime.parse("2023-07-17T12:05:11.03831+00:00", formatPattern)

额外说明

不同运行时环境对parseLenient()的宽松程度实现存在差异,这是代码在不同环境表现不一致的根本原因。想要保证跨环境兼容性,最好使用严格匹配的格式模式,同时选择正确的时间类型来对应数据。

内容的提问来源于stack exchange,提问作者Lavan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:05:24