Zab*_*uza 6
怎么了?
我在我的代码中做错了什么。
您的代码指定了模式dd-MMM-uuuu
,但您尝试解析2021-10-31
根本不符合此模式的文本。
您的字符串的正确模式是yyyy-MM-dd
. 有关详细信息,请参阅格式化程序的文档。
特别是,注意日期和月份dd-MMM
与MM-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
发布评论