Java日期时间格式
我试图读取一个CSV文件,其中我需要解析某个日期值。 但是我得到了一个DateTimeParseException,我无法放置 我试图读取的字段是一个日期字段,如下所示:Java日期时间格式,java,datetime,Java,Datetime,我试图读取一个CSV文件,其中我需要解析某个日期值。 但是我得到了一个DateTimeParseException,我无法放置 我试图读取的字段是一个日期字段,如下所示: 2016年5月8日 我使用的代码是: LocalDate-date=LocalDate.parse(模式(“d-M-yy”)的第[3]行,DateTimeFormatter.of)其中行[3]是我从CSV文件中读取的字段 我得到的例外情况是:无法在索引3处解析文本“08-May-16” 我认为d-M-yy应该能够读取一年中月份
2016年5月8日
我使用的代码是:
LocalDate-date=LocalDate.parse(模式(“d-M-yy”)的第[3]行,DateTimeFormatter.of)代码>其中行[3]
是我从CSV文件中读取的字段
我得到的例外情况是:无法在索引3处解析文本“08-May-16”
我认为d-M-yy
应该能够读取一年中月份的单位数、双位数和字母表示,这是错误的吗
编辑:我在月份中尝试了MMM-MM-M,在日期字段中尝试了dd-d。我仍然得到同样的例外
我认为d-M-yy应该能够读取一年中月份的一位数、两位数和字母表示,这是错误的吗
对。您的模式将正确解析表1-5-16中的日期,但不是2016年5月10日。请尝试dd-MMM-yyyy
我认为d-M-yy应该能够读取一年中月份的一位数、两位数和字母表示,这是错误的吗
对。您的模式将正确解析表1-5-16中的日期,但不是2016年5月10日。请尝试dd MMM yyyy
这里的问题是,在创建DateTimeFormatter
时,您没有提供Locale
-您需要使用支持该格式的Locale
:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH)));
输出:
2016-05-08
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)
这不起作用:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-LLL-yyyy", Locale.ENGLISH)));
输出:
2016-05-08
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)
这里的问题是,在创建DateTimeFormatter
时,您没有提供Locale
——您需要使用支持该格式的Locale
:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH)));
输出:
2016-05-08
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)
这不起作用:
System.out.println(LocalDate.parse("08-May-2016", DateTimeFormatter.ofPattern("dd-LLL-yyyy", Locale.ENGLISH)));
输出:
2016-05-08
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)
尝试一下我的模式“dd-MMM-yyyy”
@PiXel1225,但是我仍然得到相同的异常。你读过文档了吗:?另外请注意,月份名称是地区敏感的,您可能应该@k88是的,您是对的。以文本形式解析一个月需要根据使用LLL
表示。因此,请尝试dd LLL yyyy
。请参阅尝试模式“dd MMM yyy”
@PiXel1225,但我仍然得到相同的异常。您是否阅读了文档:?另外请注意,月份名称是地区敏感的,您可能应该@k88是的,您是对的。以文本形式解析一个月需要根据使用LLL
表示。因此,请尝试dd LLL yyyy
。谢谢,这是正确的答案。正如其他人所建议的,我已经尝试过d-MMM和dd-MMM以及类似的格式,但没有成功。由于我的电脑没有设置为英语日期时间配置,所以LCALE是必需的。谢谢,这是正确的答案。正如其他人所建议的,我已经尝试过d-MMM和dd-MMM以及类似的格式,但没有成功。由于我的电脑没有设置为英语日期时间配置,因此LCALE是必需的。