gpt4 book ai didi

java - 无法解析的日期

转载 作者:行者123 更新时间:2023-11-29 07:11:21 25 4
gpt4 key购买 nike

我有一个字符串日期“31-Dec”和模式“dd-MMM”。以及下一段代码

DateFormat formatter = new SimpleDateFormat(pattern);
formatter.setTimeZone(timeZone);
formatter.parse(input);

产生异常

java.text.ParseException: Unparseable date: "31-Dec"
at java.text.DateFormat.parse(DateFormat.java:337)....

我做错了什么?

谢谢!

最佳答案

一个问题可能是您的 Locale 不是英语。试试这个:

DateFormat formatter = new SimpleDateFormat("dd-MMM", Locale.ENGLISH);
try {
System.out.println(formatter.parse("31-Dec"));
} catch (ParseException e) {
e.printStackTrace();
}

这为我返回:

Thu Dec 31 00:00:00 CET 1970

由于您在日期字符串中缺少年份,您会看到它自动插入 1970 作为年份。

关于java - 无法解析的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13623392/

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