LocalTime() 两次之间的差异

我有一个航班行程计划,我需要了解出发和到达时间之间的差异。我从数据中得到这些指定的时间作为字符串。这是我的问题:


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 小时。我该如何解决这个问题,我在这里做错了什么?


任何帮助将不胜感激。


编辑:我正在使用图表来存储我的数据。两个机场之间最短路线的示例如下:


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 个航班。上面输出的格式是(城市、出发时间、航班号、目的地、到达时间)。


MMMHUHU
浏览 238回答 2
2回答

吃鸡游戏

24 小时内的总分钟数为 1440。因此,当差异小于零(但您需要一个正数)时,您应该在结果中添加一整天:int diff = MINUTES.between(arrival, a);if (diff < 0) {&nbsp; &nbsp; diff += 1440;}您可以使用以下方法实现相同的目的:int diff = (MINUTES.between(arrival, a) + 1440) % 1440;

哈士奇WWW

手头的主要问题LocalTime只是午夜 AM 到午夜 PM 之间的时间表示,它没有超出这些范围的任何范围的概念。您真正需要的是与时间值关联的日期值,这将允许您计算超过 24 小时的持续时间。如果做不到这一点,您将需要自己“捏造”这些价值观。这意味着,当下一次少于上一次时,您将需要手动添加一个额外的时间段,可能是一天。可能有几种方法可以做到这一点,这只是其中一种。它需要一个ZonedDateTime(设置为当前日期)并将其用作“锚”日期。Time然后将每个时间表应用于此。当它检测到最后到达时间早于下一次出发时间时,它会在值中增加一天。import java.time.Duration;import java.time.LocalTime;import java.time.ZoneId;import java.time.ZonedDateTime;import java.time.format.DateTimeFormatter;import java.util.ArrayList;import java.util.List;public class Test {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");&nbsp; &nbsp; List<Schedule> route = new ArrayList<>(4);&nbsp; &nbsp; route.add(new Schedule(LocalTime.parse("1139", formatter), LocalTime.parse("1435", formatter)));&nbsp; &nbsp; route.add(new Schedule(LocalTime.parse("0906", formatter), LocalTime.parse("1937", formatter)));&nbsp; &nbsp; route.add(new Schedule(LocalTime.parse("0804", formatter), LocalTime.parse("1521", formatter)));&nbsp; &nbsp; // Anchor time...&nbsp; &nbsp; ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("UTC"));&nbsp; &nbsp; ZonedDateTime lastArrival = null;&nbsp; &nbsp; Duration totalDuration = Duration.ZERO;&nbsp; &nbsp; for (Schedule schedule : route) {&nbsp; &nbsp; &nbsp; ZonedDateTime depart = zdt.with(schedule.getDepart());&nbsp; &nbsp; &nbsp; ZonedDateTime arrive = zdt.with(schedule.getArrive());&nbsp; &nbsp; &nbsp; if (lastArrival != null) {&nbsp; &nbsp; &nbsp; &nbsp; if (lastArrival.isAfter(depart)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Most likely, we've shifted to a new day...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Updat the anchor and target values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zdt = zdt.plusDays(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; depart = depart.plusDays(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrive = arrive.plusDays(1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Duration duration = Duration.between(lastArrival, depart);&nbsp; &nbsp; &nbsp; &nbsp; totalDuration = totalDuration.plus(duration);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("...Wait for " + duration.toHoursPart() + "h " + duration.toMinutesPart() + "m");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; Duration duration = Duration.between(depart, arrive);&nbsp; &nbsp; &nbsp; System.out.println(duration.toHoursPart() + "h " + duration.toMinutesPart() + "m");&nbsp; &nbsp; &nbsp; totalDuration = totalDuration.plus(duration);&nbsp; &nbsp; &nbsp; lastArrival = arrive;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Total duration of " + totalDuration.toHoursPart() + "d " + totalDuration.toHoursPart() + "h " + totalDuration.toMinutesPart() + "m");&nbsp; }&nbsp; public static class Schedule {&nbsp; &nbsp; private LocalTime depart;&nbsp; &nbsp; private LocalTime arrive;&nbsp; &nbsp; public Schedule(LocalTime depart, LocalTime arrive) {&nbsp; &nbsp; &nbsp; this.depart = depart;&nbsp; &nbsp; &nbsp; this.arrive = arrive;&nbsp; &nbsp; }&nbsp; &nbsp; public LocalTime getDepart() {&nbsp; &nbsp; &nbsp; return depart;&nbsp; &nbsp; }&nbsp; &nbsp; public LocalTime getArrive() {&nbsp; &nbsp; &nbsp; return arrive;&nbsp; &nbsp; }&nbsp; }}哪个会输出...2h 56m...Wait for 18h 31m10h 31m...Wait for 12h 27m7h 17mTotal duration of 3d 3h 42m但为什么要这样做?因为日期/时间操作完全是一团糟,有闰秒、年份和其他愚蠢的规则,旨在阻止它陷入混乱(这就是为什么在12:00:/没有航班的原因)......而且不要让我开始夏令时...Java 日期/时间 API 为我们处理了所有这些。我选择在单个工作单元(即 UTC)中维护日期/时间信息,这允许所有值具有某种有用的含义。您可以轻松地使用不同的锚点时间以及转换为不同的时区以用于演示 - 因此您可以在目的地以当地时间显示时间,但计算将继续针对单个事实点进行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java