java.time.format.DateTimeParseException:无法解析文本“24:00:00”:HourOfDay 的无效值有效值 0 - 23:24

问题描述

我正在尝试将日期和时间值转换为 LocalDateTime:

String date = "2021-05-07";
String time = "24:00:00";
LocalDateTime start = LocalDateTime.of(LocalDate.parse(date),LocalTime.parse(time));

但是,我遇到了一个错误

java.time.format.DateTimeParseException:无法解析文本“24:00:00”:HourOfDay 的无效值(有效值 0 - 23):24

我尝试使用自己的 DateTimeFormatter:

DateTimeFormatter dtf_time = DateTimeFormatter.ofPattern("HH:mm:ss");
String date = "2021-05-07";
String time = "24:00:00";
LocalDateTime start = LocalDateTime.of(LocalDate.parse(date),dtf_time.parse(time));

但是,此 DateTimeFormatter 不适用于 LocalDateTime:

LocalDateTime 类型中的(LocalDate,LocalTime) 方法不适用于参数 (LocalDate,TemporalAccessor)

有人可以帮我吗?我需要一个 LocalDateTime-Object 来计算两个对象之间的持续时间:

Duration.between(start,end).toDays();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)