为了账号安全,请及时绑定邮箱和手机立即绑定

LocalTime() 两次之间的差异

LocalTime() 两次之间的差异

MMMHUHU 2022-01-12 17:07:20
我有一个航班行程计划,我需要了解出发和到达时间之间的差异。我从数据中得到这些指定的时间作为字符串。这是我的问题: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 Sydney1 Edinburgh, 1139, TK3245, Istanbul, 14352 Istanbul, 0906, TK4557, Singapore, 19373 Singapore, 0804, QF1721, Sydney, 1521这些是我们从 EDI 到 SYD 的 3 个航班。上面输出的格式是(城市、出发时间、航班号、目的地、到达时间)。
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

24 小时内的总分钟数为 1440。因此,当差异小于零(但您需要一个正数)时,您应该在结果中添加一整天:


int diff = MINUTES.between(arrival, a);

if (diff < 0) {

    diff += 1440;

}

您可以使用以下方法实现相同的目的:


int diff = (MINUTES.between(arrival, a) + 1440) % 1440;


查看完整回答
反对 回复 2022-01-12
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

手头的主要问题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 {


  public static void main(String[] args) {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");


    List<Schedule> route = new ArrayList<>(4);

    route.add(new Schedule(LocalTime.parse("1139", formatter), LocalTime.parse("1435", formatter)));

    route.add(new Schedule(LocalTime.parse("0906", formatter), LocalTime.parse("1937", formatter)));

    route.add(new Schedule(LocalTime.parse("0804", formatter), LocalTime.parse("1521", formatter)));


    // Anchor time...

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("UTC"));

    ZonedDateTime lastArrival = null;

    Duration totalDuration = Duration.ZERO;

    for (Schedule schedule : route) {

      ZonedDateTime depart = zdt.with(schedule.getDepart());

      ZonedDateTime arrive = zdt.with(schedule.getArrive());


      if (lastArrival != null) {

        if (lastArrival.isAfter(depart)) {

          // Most likely, we've shifted to a new day...

          // Updat the anchor and target values

          zdt = zdt.plusDays(1);

          depart = depart.plusDays(1);

          arrive = arrive.plusDays(1);

        }

        Duration duration = Duration.between(lastArrival, depart);

        totalDuration = totalDuration.plus(duration);

        System.out.println("...Wait for " + duration.toHoursPart() + "h " + duration.toMinutesPart() + "m");

      }


      Duration duration = Duration.between(depart, arrive);

      System.out.println(duration.toHoursPart() + "h " + duration.toMinutesPart() + "m");

      totalDuration = totalDuration.plus(duration);


      lastArrival = arrive;

    }

    System.out.println("Total duration of " + totalDuration.toHoursPart() + "d " + totalDuration.toHoursPart() + "h " + totalDuration.toMinutesPart() + "m");


  }


  public static class Schedule {


    private LocalTime depart;

    private LocalTime arrive;


    public Schedule(LocalTime depart, LocalTime arrive) {

      this.depart = depart;

      this.arrive = arrive;

    }


    public LocalTime getDepart() {

      return depart;

    }


    public LocalTime getArrive() {

      return arrive;

    }


  }

}

哪个会输出...


2h 56m

...Wait for 18h 31m

10h 31m

...Wait for 12h 27m

7h 17m

Total duration of 3d 3h 42m

但为什么要这样做?因为日期/时间操作完全是一团糟,有闰秒、年份和其他愚蠢的规则,旨在阻止它陷入混乱(这就是为什么在12:00:/没有航班的原因)......而且不要让我开始夏令时...


Java 日期/时间 API 为我们处理了所有这些。


我选择在单个工作单元(即 UTC)中维护日期/时间信息,这允许所有值具有某种有用的含义。您可以轻松地使用不同的锚点时间以及转换为不同的时区以用于演示 - 因此您可以在目的地以当地时间显示时间,但计算将继续针对单个事实点进行。


查看完整回答
反对 回复 2022-01-12
  • 2 回答
  • 0 关注
  • 237 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号