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

Java中无法解析带年份的ISO-8601时长字符串(如P10YT10M)的问题求助

Java中无法解析带年份的ISO-8601时长字符串(如P10YT10M)的问题求助

大家好,我最近在处理ISO-8601格式的时长字符串时遇到了麻烦:尝试用Java的java.time.Duration.parse()方法解析类似P10YT10M这样包含年份的时长字符串时,总是抛出异常,报错提示*"Text cannot be parsed to a Duration"*。

我的测试代码是这样的:

try {
    String durationString = "P5YT30M";
    Duration duration = Duration.parse(durationString);
} catch (Exception e) {
    System.err.println(e.getMessage());
}

奇怪的是,像PT5M这种只包含分钟的短时长字符串是可以正常解析的,但只要时长里包含年份(Y)或者月份(M)这类更大的时间单位,解析就会失败。有没有朋友知道这是为什么,以及该怎么解决这个问题呢?

备注:内容来源于stack exchange,提问作者Vaibhav R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 16:09:41