解析 LocalDate 但得到 DateTimeParseException;dd

互联网 行业动态 更新时间:2024-06-13 00:19:32

Zab*_*uza 6

怎么了?

我在我的代码中做错了什么。

您的代码指定了模式dd-MMM-uuuu,但您尝试解析2021-10-31根本不符合此模式的文本。

您的字符串的正确模式是yyyy-MM-dd. 有关详细信息,请参阅格式化程序的文档。

特别是,注意日期和月份dd-MMMMM-dd. 又额月MMM。与您当前模式匹配的字符串将是31-Oct-2021.


改变模式

从评论:

我的输入日期是 - 2021-10-31 需要转换为 - 2021 年 10 月 31 日

您可以通过以下方式轻松更改日期模式:

使用模式解析输入日期yyyy-MM-dd 然后使用模式将其格式化回字符串dd-MMM-yyyy

在代码中,即:

DateTimeFormatter inputPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter outputPattern = DateTimeFormatter.ofPattern("dd-MMM-yyyy");

String input = "2021-10-31";
LocalDate date = LocalDate.parse(text, inputPattern);

String output = date.format(outputPattern);

更多推荐

LocalDate,DateTimeParseException,dd

本文发布于:2023-04-21 09:46:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/8fa79cc8fe81ce408fffcb6eddfc2ed8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LocalDate   DateTimeParseException   dd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!