当前位置: 首页 > 面试题库 >

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

夏俊杰
2023-03-14
问题内容

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

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

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

但是当我使用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

同样奇怪的是,当我执行反向操作format时,它运行良好:

 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

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

  1. 如果每天至少有一个小时可用,则会形成LocalTime。这涉及提供分钟,秒和秒的默认值。

我了解的是,必须每天使用小时字段来解析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


 类似资料:
  • 我在这里有我的日期来自我的API,我试图在其中解析到。我使用intl包尝试了以下方法,但我遇到了错误 未处理的异常:格式异常:尝试从 2020/07/07 09:47:54 在位置 20 读取 有人知道这里出了什么问题吗?

  • 我需要在Dart中的对象中解析这种日期“Mon,2014年8月11日12:53 pm PDT”。 有一个静态方法,它接受ISO 8601格式的子集,而不是我的情况。 类允许您定义要分析的日期模式。我创建了一个模式“EEE,dd-MMM-yyy-hh:mm-a-zzz”。 使用它,我得到一个读取一个。 看起来解析器不喜欢PM标记的情况(我已经提出了一个问题)。 我试着解决整个管柱上套管的问题。使用字

  • 问题内容: 我的日期格式为dd-mmm-yy或d- mmm-y,其中月份是字母的缩写(例如,09年11月4日或05年12月12日等)。喜欢解析它以产生一个java.util.Date对象。 可以通过利用java.text.DateFormat类来实现吗?还是有另一种简单的方法? 问题答案: 您可能需要使用SimpleDateFormat来解析自定义格式。此文章解释格式的细节。

  • 嗨,我正在尝试以YYYY-MM-DD格式解析日期。我正在尝试这个: 但是我得到了错误,我想这是一个解析错误, 0001-01-01 00:00:00 0000 UTC解析时间“2016-01-1”为“2020-12-30”:无法将“-01-01”解析为“0” 我该如何解析它,或者通过其他示例给出一些提示?蒂亚。

  • 我试图以C#为目标,使用Antlr4解析一个日期。在我的情况下,有效日期应具有以下内容 采用格式 年份只能有4位数字 月和日只能有2位 我知道类似的问题已经出现了,但它们的解决方案似乎对我不起作用 如何创建将解析日期的antlr4语法 ANTLR:识别日期和数字的最简单方法? 我在某个地方读到过,有一种类似优先级的解析,其中基于语法文件如何编写的顶级规则首先被评估。因此,考虑一下,除了日期,我的语

  • 问题内容: 我有两个时间戳,它们以两种不同的格式描述同一时刻。 和。 我用Joda-Time用两个不同的日期格式解析时间戳。最后,我想有两个DateTime对象,它们在同一时刻相同。 DateFormatter提供了几种控制时区和语言环境的方法,但我无法使其正常工作。 这是我想工作的代码: 问题答案: 如果您的默认时间zome是Europe / Berlin,则2010-10-03 18:58:0