java JDK8:无法解析LocalTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30754259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:36:30  来源:igfitidea点击:

JDK8: unable to parse LocalTime

javajava-8date-parsing

提问by Luigi Cortese

I managed to parse a Stringto a LocalDateobject:

我设法将 a 解析String为一个LocalDate对象:

DateTimeFormatter f1=DateTimeFormatter.ofPattern("dd MM yyyy");
LocalDate d=LocalDate.parse("26 08 1984",f1);
System.out.println(d); //prints "1984-08-26"

But I cannot do the same with LocalTime. This piece of code:

但我不能对LocalTime. 这段代码:

DateTimeFormatter f2=DateTimeFormatter.ofPattern("hh mm");
LocalTime t=LocalTime.parse("11 08",f2); //exception here
System.out.println(t);

Throws a DateTimeParseException:

抛出一个DateTimeParseException

Exception in thread "main" java.time.format.DateTimeParseException: Text '11 08' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {MinuteOfHour=8, HourOfAmPm=11},ISO of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalTime.parse(Unknown Source)
    at com.mui.cert.Main.<init>(Main.java:21)
    at com.mui.cert.Main.main(Main.java:12)
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MinuteOfHour=8, HourOfAmPm=11},ISO of type java.time.format.Parsed
    at java.time.LocalTime.from(Unknown Source)
    at java.time.LocalTime$$Lambda/1854731462.queryFrom(Unknown Source)
    at java.time.format.Parsed.query(Unknown Source)
    ... 4 more

What am I doing wrong?

我究竟做错了什么?

回答by Jordi Castilla

If you use a specific format, according to API:

如果您使用特定格式,根据API

The string must represent a valid time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME.

该字符串必须表示有效时间并使用DateTimeFormatter.ISO_LOCAL_TIME.

hh mm 

for 24h must be

24h 必须是

HH mm

or for 12h

或 12 小时

kk mm

The handled formats must have this conditions:

处理的格式必须具有以下条件:

  • Two digits for the hour-of-day. This is pre-padded by zero to ensure two digits.
  • A colon
  • Two digits for the minute-of-hour. This is pre-padded by zero to ensure two digits.
  • If the second-of-minute is not available then the format is complete.
  • A colon
  • Two digits for the second-of-minute. This is pre-padded by zero to ensure two digits.
  • If the nano-of-second is zero or not available then the format is complete.
  • A decimal point
  • One to nine digits for the nano-of-second. As many digits will be output as required.
  • 两位数表示一天中的小时。这是预先填充零以确保两位数。
  • 冒号
  • 小时的两位数。这是预先填充零以确保两位数。
  • 如果分钟的秒数不可用,则格式已完成。
  • 冒号
  • 分钟秒的两位数。这是预先填充零以确保两位数。
  • 如果纳秒为零或不可用,则格式完成。
  • 一个小数点
  • 纳秒的一到九位数字。将根据需要输出尽可能多的数字。

回答by walkeros

Use DateTimeFormatter.ofPattern("kk mm"); for 12 hour clock or DateTimeFormatter.ofPattern("HH mm")for 24 hour clock

使用DateTimeFormatter.ofPattern("kk mm"); 12 小时制或DateTimeFormatter.ofPattern("HH mm")24 小时制

If you want to parse time with hhyou must combine it wih awhere you define AM or PM:

如果你想解析时间,hh你必须将它与a你定义 AM 或 PM 的地方结合起来:

DateTimeFormatter f2 = DateTimeFormatter.ofPattern("hh mm a");
LocalTime t = LocalTime.parse("11 08 AM", f2);

回答by Alex Taylor

In this case Unable to obtain LocalTime from TemporalAccessormeans it cannot determine the how far through the day the given string represents i.e. there is not enough information to construct a LocalTime. Behind the scenes, the code looks something like this expanded Java 8 version (which gives a similar error):

在这种情况下,Unable to obtain LocalTime from TemporalAccessor意味着它无法确定给定字符串表示的一天有多远,即没有足够的信息来构建LocalTime. 在幕后,代码看起来像这个扩展的 Java 8 版本(它给出了类似的错误):

DateTimeFormatter f2 = DateTimeFormatter.ofPattern("hh mm");
TemporalAccessor temporalAccessor = f2.parse("11 08");
LocalTime t = temporalAccessor.query(LocalTime::from);
System.out.println(t);

The LocalTime::fromdocumentation says

LocalTime::from文件说,

The conversion uses the TemporalQueries.localTime() query, which relies on extracting the NANO_OF_DAY field.

转换使用 TemporalQueries.localTime() 查询,它依赖于提取 NANO_OF_DAY 字段。

Your error is telling you that the TemporalAccessoronly has two fields, neither of which is a NANO_OF_DAYfield. The minimum allowable patterns for retrieving a LocalTimeusing a DateTimeFormatterare:

您的错误是告诉您TemporalAccessoronly 有两个字段,这两个字段都不是NANO_OF_DAY字段。LocalTime使用 a检索 a 的最小允许模式DateTimeFormatter是:

DateTimeFormatter.ofPattern("ha");
DateTimeFormatter.ofPattern("Ka");
DateTimeFormatter.ofPattern("ah");
DateTimeFormatter.ofPattern("aK");
DateTimeFormatter.ofPattern("k");
DateTimeFormatter.ofPattern("H");

Your pattern must contain at least one of those strings to get a NANO_OF_DAYfield in the internal TemporalAccessorfrom which a LocalTimecan be constructed.

您的模式必须至少包含这些字符串中的一个,才能NANO_OF_DAY在内部TemporalAccessor获取一个LocalTime可以构造a 的字段。

回答by dogant

You need to use capital HH in the pattern

您需要在模式中使用大写 HH

DateTimeFormatter f2=DateTimeFormatter.ofPattern("HH mm");

or do this, for clock-hour-of-am-pmyou need to specify it.

或者这样做,因为clock-hour-of-am-pm您需要指定它。

This should also work

这也应该有效

DateTimeFormatter f2=DateTimeFormatter.ofPattern("hh mm a");
LocalTime t=LocalTime.parse("11 08 AM",f2); //exception here