LocalTime()两次之间的时差 [英] LocalTime() difference between two times
问题描述
我有一个航班行程计划,需要获取出发时间和到达时间之间的差额.我从数据中获得这些指定的时间作为String.这是我的问题:
I have a flight itinerary program where I need to get difference between departure and arrival time. I get these specified times as String from the data. Here is my problem:
import static java.time.temporal.ChronoUnit.MINUTES;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class test2 {
public static void main(String[] args) {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("HHmm");
LocalTime departure = LocalTime.parse("1139", dateFormat);
LocalTime arrival = LocalTime.parse("1435", dateFormat);
LocalTime a = LocalTime.parse("0906", dateFormat);
System.out.println(MINUTES.between(departure, arrival));
System.out.println(MINUTES.between(arrival, a));
}
}
输出:
176
-329
第一次返回11:39和14:35之间的差值就好了.但是第二个区别只是5小时,而应该是19小时.我该怎么解决,我在做什么错了?
The first time returns the difference between 11:39 and 14:35 just fine. But the second difference is only 5 hours while it should be 19 hours. How can I fix this, what am I doing wrong here?
任何帮助将不胜感激.
Any help would be greatly appreciated.
我正在使用图形存储数据.两个机场之间最短路线的示例如下:
I am using graphs to store my data in. An example of a shortest route between two airports is like this:
Route for Edinburgh to Sydney
1 Edinburgh, 1139, TK3245, Istanbul, 1435
2 Istanbul, 0906, TK4557, Singapore, 1937
3 Singapore, 0804, QF1721, Sydney, 1521
这些是使我们从EDI飞往SYD的3航班.上面输出的格式是(城市,出发时间,航班号,目的地,到达时间).
These are the 3 flights that takes us from EDI to SYD. The format of the output above is (City, Departure Time, Flight No., Destination, Arrival Time).
推荐答案
24小时内的总分钟数是1440.因此,当差异小于零(但您需要一个正数)时,您应该添加一整天到您的结果:
The total number of minutes in 24 hours is 1440. So when the difference is below zero (but you need a positive one) then you should add a whole day to your result:
int diff = MINUTES.between(arrival, a);
if (diff < 0) {
diff += 1440;
}
您可以使用以下方法实现相同的目的:
You can achieve the same thing using this:
int diff = (MINUTES.between(arrival, a) + 1440) % 1440;
这篇关于LocalTime()两次之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!