gpt4 book ai didi

java - 为什么这个 SimpleDateFormat 对象的 parse 方法没有抛出 ParseException?

转载 作者:行者123 更新时间:2023-12-01 12:08:17 25 4
gpt4 key购买 nike

是的,我已经关闭了宽松选项:

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);

String candidateDate = "1-1-1";

try {
System.out.println(df.parse(candidateDate));
} catch (ParseException ex) {
System.out.println(ex.getMessage());
}

我在控制台中看到:

Sat Jan 01 00:00:00 CAT 1

最佳答案

格式字符的数量并不明确指示要解析的位数。

具体示例来自API关于年份字段:

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits.

关于java - 为什么这个 SimpleDateFormat 对象的 parse 方法没有抛出 ParseException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com