简体   繁体   English

JDK8:无法解析LocalTime

[英]JDK8: unable to parse LocalTime

I managed to parse a String to a LocalDate object: 我设法将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 . 但我不能对LocalTime做同样的事情。 This piece of code: 这段代码:

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$15/1854731462.queryFrom(Unknown Source)
    at java.time.format.Parsed.query(Unknown Source)
    ... 4 more

What am I doing wrong? 我究竟做错了什么?

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 必须24小时

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. 将根据需要输出许多数字。

Use DateTimeFormatter.ofPattern("kk mm") ; 使用DateTimeFormatter.ofPattern("kk mm") ; for 12 hour clock or DateTimeFormatter.ofPattern("HH mm") for 24 hour clock 12小时时钟或DateTimeFormatter.ofPattern("HH mm") 24小时制

If you want to parse time with hh you must combine it wih a where you define AM or PM: 如果你想用hh解析时间,你必须将它a你定义AM或PM a地方结合起来:

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

In this case Unable to obtain LocalTime from TemporalAccessor means it cannot determine the how far through the day the given string represents ie there is not enough information to construct a LocalTime . 在这种情况下, Unable to obtain LocalTime from TemporalAccessor意味着它无法确定给定字符串表示的一天中的多少,即没有足够的信息来构造LocalTime Behind the scenes, the code looks something like this expanded Java 8 version (which gives a similar error): 在幕后,代码看起来像这个扩展的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::from documentation 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 TemporalAccessor only has two fields, neither of which is a NANO_OF_DAY field. 您的错误告诉您TemporalAccessor只有两个字段,这两个字段都不是NANO_OF_DAY字段。 The minimum allowable patterns for retrieving a LocalTime using a DateTimeFormatter are: 使用DateTimeFormatter检索LocalTime的最小允许模式是:

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_DAY field in the internal TemporalAccessor from which a LocalTime can be constructed. 你的模式必须包含这些字符串中的至少一个获得NANO_OF_DAY在内部场TemporalAccessor从一个LocalTime可以构造。

You need to use capital HH in the pattern 您需要在模式中使用大写HH

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

or do this, for clock-hour-of-am-pm you 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM