我正在尝试读取CSV文件,在其中我需要解析某个日期值。然而,我得到了一个我无法放置的DateTimeParseException。
我试图读取的字段是一个日期字段,它看起来如下所示:08-May-2016
我使用的代码:LocalDate date = LocalDate.parse(row[3], DateTimeFormatter.ofPattern("d-M-yy"));,其中row[3]是我从CSV文件中读取的字段。
我得到的例外:Text '08-May-16' could not be parsed at index 3
我认为d-M-yy应该能够读懂一年中的单数、双位数和字母表示,这是错误的吗?
编辑:我已经尝试了的月份,以及dd-d的日字段。我还是有同样的例外。
发布于 2016-05-10 13:07:44
这里的问题是在创建Locale时没有提供DateTimeFormatter --您需要使用支持这种格式的Locale:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH)));输出:
2016-05-08这不管用:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-LLL-yyyy", Locale.ENGLISH)));输出:
Exception in thread "main" java.time.format.DateTimeParseException: Text '08-May-2016' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)发布于 2016-05-10 11:31:56
难道我认为d-M应该能够读懂一年中的单数、双位数和字母表示的想法是错误的吗?
是。您的模式将正确解析表格1-5-16的日期,但不能解析10-5-2016年的日期。试试dd-MMM-yyyy。
https://stackoverflow.com/questions/37137168
复制相似问题