Java JDK8:无法分析LocalTime

Java JDK8:无法分析LocalTime,java,java-8,date-parsing,Java,Java 8,Date Parsing,我设法将字符串解析为LocalDate对象: DateTimeFormatter f1=DateTimeFormatter.ofPattern("dd MM yyyy"); LocalDate d=LocalDate.parse("26 08 1984",f1); System.out.println(d); //prints "1984-08-26" 但是我不能用LocalTime做同样的事情。这段代码: DateTimeFormatter f2=DateTimeFormatter.ofPa

我设法将
字符串
解析为
LocalDate
对象:

DateTimeFormatter f1=DateTimeFormatter.ofPattern("dd MM yyyy");
LocalDate d=LocalDate.parse("26 08 1984",f1);
System.out.println(d); //prints "1984-08-26"
但是我不能用
LocalTime
做同样的事情。这段代码:

DateTimeFormatter f2=DateTimeFormatter.ofPattern("hh mm");
LocalTime t=LocalTime.parse("11 08",f2); //exception here
System.out.println(t);
抛出一个
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
线程“main”java.time.format.DateTimeParseException中的异常:无法分析文本“11 08”:无法从TemporalAccessor获取LocalTime:{MinuteOfHour=8,HourOfAmPm=11},类型为java.time.format.parsed的ISO 位于java.time.format.DateTimeFormatter.createError(未知源) 位于java.time.format.DateTimeFormatter.parse(未知源) at java.time.LocalTime.parse(未知源) com.mui.cert.Main.(Main.java:21) 位于com.mui.cert.Main.Main(Main.java:12) 原因:java.time.DateTimeException:无法从TemporalAccessor获取LocalTime:{MinuteOfHour=8,HourOfAmPm=11},类型为java.time.format.Parsed的ISO at java.time.LocalTime.from(未知源) 位于java.time.LocalTime$$Lambda$15/1854731462.queryFrom(未知源) at java.time.format.Parsed.query(未知源) ... 4更多
我做错了什么?

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

DateTimeFormatter f2=DateTimeFormatter.ofPattern("HH mm");
或者这样做,对于am pm的
时钟小时
,您需要指定它

这也应该起作用

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

使用模式的日期时间格式(“kk mm”);对于12小时制或
模式的日期时间格式(“HH-mm”)
对于24小时制

如果要使用
hh
解析时间,必须将其与定义AM或PM的
a
组合:

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

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

字符串必须表示有效时间,并使用解析

24小时内必须

HH mm
还是12小时

kk mm
处理的格式必须满足以下条件:

  • 两位数字表示一天中的小时。这是预先填充的零,以确保两位数字
  • 冒号
  • 两位数字表示小时分钟。这是预先填充的零,以确保两位数字
  • 如果第二分钟不可用,则格式已完成
  • 冒号
  • 两位数字表示一分钟的第二位。这是预先填充的零,以确保两位数字
  • 如果nano of second为零或不可用,则格式已完成
  • 小数点
  • 一到九个数字表示纳秒。将根据需要输出尽可能多的数字

在这种情况下,
无法从TemporalAccessor获取LocalTime
意味着它无法确定给定字符串在一天中所代表的距离,即没有足够的信息来构建
LocalTime
。在幕后,代码看起来类似于这个扩展的Java 8版本(它给出了一个类似的错误):

文件上说

转换使用TemporalQueries.localTime()查询,该查询 依赖于提取天字段的NANO_

您的错误是告诉您,
TemporalAccessor
只有两个字段,这两个字段都不是
NANO\u of_DAY
字段。使用
DateTimeFormatter
检索
LocalTime
的最小允许模式为:

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

您的模式必须至少包含这些字符串中的一个,才能在内部
TemporalAccessor
中获得一个
NANO\u of_DAY
字段,从该字段可以构建
LocalTime

它可以工作。。。但是为什么呢
h->am-pm的时钟时间
也应该工作!我认为,
am-pm
部分很重要-它可以引用两个不同的本地时间(一个用于am,一个用于pm)您可以链接资源吗?这很有趣,我认为我可以无限期地扰乱元素的顺序。。。
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("hh mm");
TemporalAccessor temporalAccessor = f2.parse("11 08");
LocalTime t = temporalAccessor.query(LocalTime::from);
System.out.println(t);
DateTimeFormatter.ofPattern("ha");
DateTimeFormatter.ofPattern("Ka");
DateTimeFormatter.ofPattern("ah");
DateTimeFormatter.ofPattern("aK");
DateTimeFormatter.ofPattern("k");
DateTimeFormatter.ofPattern("H");