LocalTime() difference between two times

前端 未结 2 1306
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 06:22

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:

2条回答
  •  [愿得一人]
    2021-01-18 07:02

    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;
    

提交回复
热议问题