使用Java 8 DateTime API解析日期和AM/PM [英] Parsing date and AM/PM using java 8 DateTime API
问题描述
我正在尝试使用Java 8 DateTime API解析日期时间字符串.
I'm trying to parse a date time string using java 8 DateTime API.
要分析的字符串包含日期,月份,年份和AM/PM标记,例如17/02/2015 PM
.
我使用以下模式:dd/MM/yyyy aa
,并且我希望将解析的LocalDateTime时间部分设置为12:00:00
或00:00:00
,具体取决于AM/PM标记.
The string to parse contains date, month, year and AM/PM marker such as 17/02/2015 PM
.
I use the following pattern: dd/MM/yyyy aa
and I expect the parsed LocalDateTime time part to be set at 12:00:00
or 00:00:00
depending on the AM/PM marker.
使用以前的java.text.SimpleDateFormat
API,使用此模式进行解析可以按预期进行.
Using the former java.text.SimpleDateFormat
API, the parsing works as expected using this pattern.
但是当我使用Java 8 DateTime API并运行以下代码时:
But when i use java 8 DateTime API and run the following code:
LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa"));
我遇到以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1765)
at java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1604)
at java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1572)
at java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:534)
如果我将模式切换为dd/MM/yyyy a
,则出现以下异常:
If i switch the pattern to dd/MM/yyyy a
then i got the folowing exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '17/02/2015 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.LocalDateTime$$Lambda$7/474675244.queryFrom(Unknown Source)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 5 more
奇怪的是,当我执行反向操作格式化时,它工作得很好:
It's also weird that when i do the reverse operation, format, it works fine:
System.out.println(LocalDateTime.of(2015, 02, 17, 0, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
System.out.println(LocalDateTime.of(2015, 02, 17, 12, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
分别打印17/02/2015 AM
和17/02/2015 PM
.
java.time.DateTimeFormatter
的Javadoc说(§解析,步骤#6):
The javadoc for java.time.DateTimeFormatter
says (§Resolving, step #6):
- 如果每天至少有一个小时可用,则会形成LocalTime.这涉及为分钟,秒和秒的小数部分提供默认值.
我了解的是,必须每天使用小时字段来解析LocalDateTime ...仅使用AM/PM字段来解析时间部分是没有办法吗?
What i understand is that the field hour-of-day is mandatory to parse a LocalDateTime... Isn't there any way to parse the time part only using the AM/PM field only?
推荐答案
You can set the default values for unavailable fields via DateTimeFormatterBuilder.parseDefaulting
:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd/MM/yyyy a")
.parseDefaulting(ChronoField.HOUR_OF_AMPM, 0) // this is required
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) // optional, but you can set other value
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) // optional as well
.toFormatter();
System.out.println(LocalDateTime.parse("17/02/2015 PM", formatter)); // 2015-02-17T12:00
System.out.println(LocalDateTime.parse("17/02/2015 AM", formatter)); // 2015-02-17T00:00
这篇关于使用Java 8 DateTime API解析日期和AM/PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!