java - 使用 java 8 DateTime API 解析日期和 AM/PM

标签 java java-8 java-time

我正在尝试使用 java 8 DateTime API 解析日期时间字符串。

要解析的字符串包含日期、月份、年份和 AM/PM 标记,例如 17/02/2015 PM。 我使用以下模式:dd/MM/yyyy aa 并且我希望将解析的 LocalDateTime 时间部分设置为 12:00:0000:00 :00 取决于 AM/PM 标记。

使用以前的 java.text.SimpleDateFormat API,解析按使用此模式的预期进行。

但是当我使用 java 8 DateTime API 并运行以下代码时:

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 然后我得到以下异常:

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

同样奇怪的是,当我进行反向操作时,格式化,它工作正常:

 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 AM17/02/2015 PM

java.time.DateTimeFormatter 的 javadoc 说(§Resolving,步骤 #6):

  1. A LocalTime is formed if there is at least an hour-of-day available. This involves providing default values for minute, second and fraction of second.

我的理解是,字段 hour-of-day 是解析 LocalDateTime 所必需的...难道没有任何方法可以仅使用 AM/PM 字段来解析时间部分吗?

最佳答案

您可以通过 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 - 使用 java 8 DateTime API 解析日期和 AM/PM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32180044/

相关文章:

java - 以本地化顺序列出 <DayOfWeek>

java - 在 Java、Hibernate 和 MySQL 中,ñ 等于 n

java - glReadPixels() 返回零数组

java - 如何使用java流创建一个固定长度和特定数字的int[]?

java - 三元运算符在 JDK8 和 JDK10 上的行为差异

java - Json 解析错误 - MismatchedInputException 在 Spring Boot 中期望 LocalDate 的数组或字符串

java-8 - Java 8 DateTimeFormatter 两位数的年份 18 解析为 0018 而不是 2018?

java - 使用 JPA 持久化第三方对象

java - 使用 springmvc 和 hibernate 运行我的 Maven 项目时出错

Java 8 流媒体 : How to convert list of objects to a list of its selected properties