您好,我是来解决 Locale 格式化程序语言的 DateTimeFormatter 问题的,让我向您介绍一下:
- 我正在使用以下字符串格式读取 CSV 文件:“dd-MMM-yyyy”<-->“28-dic-2017”
- 然后我必须将列表中的每个字符串解析为 LocalDate 以使用 timeAPI 方法处理每个字符串。
- 请注意,我的月份是西班牙语缩写的。那是一团糟!
- 执行时返回:
观察到堆栈跟踪错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '28-dic-2017' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953)
at java.base/java.time.LocalDate.parse(LocalDate.java:429)
at com.examen.Presentation.Principal.parseDate(Principal.java:28)
at com.examen.Presentation.Principal.main(Principal.java:19)
最小的、可重现的代码示例:
public static LocalDate parseDate(String fecha) {
Locale loc = new Locale("es", "ES");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", loc);
LocalDate date = LocalDate.parse(fecha, formatter);
return date;
}
我从语言环境尝试不同的事情和方法时遇到了同样的错误,而 DateTimeFormatter 甚至不知道我在做什么,但我认为这显然不应该起作用。