如何使用<hour>h<minute>格式Java Netbeans计算时差



我目前正在试验机场自助值机项目。

我想做的是使用格式(小时(h(分钟(输入出发时间示例(14h40(

我必须通过检查小时值是否小于或等于23以及分钟值是否小于等于59来验证这一点。

如果这是有效的,那么我想计算登机时间,即起飞时间前35分钟。

有什么想法吗?

我正在使用的GUI。https://gyazo.com/62dd8ea5a2ff7cd04aa777447679bcf8

最好创建一个LocalTime,然后每件事对你来说都很容易,要做到这一点,你可以使用:

String time = "14h40";
String[] hm = time.split("h");
LocalTime departureTime = null;
try {
departureTime = LocalTime.of(Integer.valueOf(hm[0]), Integer.valueOf(hm[1]));
} catch (DateTimeException e) {
// time is not correct, throw an exception!
throw new IllegalArgumentException("Time is not correct");
}
// do calculation of the boarding time. 
LocalTime boardingTime = departureTime.minusMinutes(35);

或者如Ole V.V.所述,您可以使用DateTimeFormatter:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H'h'mm");
LocalTime departureTime = LocalTime.parse("4h40", formatter);