提问人:Maxim 提问时间:3/11/2023 最后编辑:AnonymousMaxim 更新时间:3/11/2023 访问量:134
Java - DateTimeFormatter 不适用于德语日期字符串 [duplicate]
Java - DateTimeFormatter not working for German Date String [duplicate]
问:
我正在尝试以这种格式解析日期字符串:“18-Feb-23”。该月是德语。我试过了这个:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-LLL-yy", Locale.GERMAN);
LocalDate.parse(row.soldDate,formatter),
但我得到这个例外:
Caused by: java.time.format.DateTimeParseException: Text '18-Feb-23' could not be parsed at index 0
我也尝试过 d-MMM-yy,但这不起作用。
答:
1赞
Sakshum Sharma
3/11/2023
#1
刚刚试用了您的代码,对我来说效果很好
public void func () {
String date = "18-Mai-23";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-LLL-yy", Locale.GERMAN);
LocalDate localDate = LocalDate.parse(date,formatter);
System.out.println(localDate);
}
这是输出
2023-05-18
从您分享的内容中很难猜测,但我认为在您的案例中要进入日期格式化的字符串是不纯的。也就是说,它包含一些字符(甚至可能在输出窗口中不可见),这些字符会把事情搞砸。您可以尝试以下操作
- 不要使用“row.solDate”中的数据,而是尝试传递硬编码的字符串。如果这可行,则 row.solDate 中的数据是错误的
- 检查编码是否相似,即 UTF-8 等编码协议在应用程序的写入和读取部分中是相似的。
0赞
mi1000
3/11/2023
#2
确实有可能你有BOM或类似的东西,你应该打印出每个字符,以确保你的数据没有错误,并验证编码是否正确,如果没有,那么例如使用Notepad ++将其转换为无BOM的文件,这样你就不会再遇到这个问题了。
评论
row.soldDate
"18-Feb-23"
row.soldDate
-
\000
)row
row.soldDate.chars().forEach(System.out::println);