两个DateTime对象之间的差异,以分钟为单位

时间:2018-06-16 01:32:04

标签: java datetime localdate

===========================================================
Date_Time_from          | Date_Time_to
===========================================================
2018-06-16 02:00:00     |2018-06-18 02:00:00
2018-06-16 03:00:00     |2018-06-18 03:00:00
2018-06-16 04:00:00     |2018-06-18 04:00:00
2018-06-16 05:00:00     |2018-06-18 05:00:00
2018-06-16 06:00:00     |2018-06-18 06:00:00
==========================================================

我使用的是LocalDateTime,但它没有得到2 DateTime的差异,而是只计算时间。这不算数日。

以下是代码:

LocalDateTime fromdate = LocalDateTime.of(orderYear,orderMonth,orderDay,orderHour,orderMinute,orderSeconds);
LocalDateTime todate = LocalDateTime.of(deliverYear, deliverMonth, deliverDay,deliverHour,deliverMinute,deliverSeconds);
int a = fromdate.get(ChronoField.MINUTE_OF_DAY)-todate.get(ChronoField.MINUTE_OF_DAY);

上述代码的输出为零,因为在date_time_from中相当于120分钟的2小时内减去date_time_to中的2小时。

是否有其他方法可以获得总分钟数并包括计算日期?

3 个答案:

答案 0 :(得分:3)

LocalDateTime fromdate = LocalDateTime.of(orderYear,orderMonth,orderDay,orderHour,orderMinute,orderSeconds);
LocalDateTime todate = LocalDateTime.of(deliverYear, deliverMonth, deliverDay,deliverHour,deliverMinute,deliverSeconds);
Duration difference = Duration.between(fromdate, todate);


然后你可以使用类似于...的东西来格式化它。

long hours = difference.toHours();
long mins = difference.minusHours(hours).toMinutes();

// Or if you're lucky enough to be using Java 9+
//String formatted = String.format("%dhrs %02dmins", duration.toHours(), duration.toMinutesPart());
String formatted = String.format("%dhrs %02dmins", hours, mins);


只是为了证明这一点......

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        List<LocalDateTime> from = new ArrayList<>(5);
        List<LocalDateTime> to = new ArrayList<>(5);

        from.add(LocalDateTime.parse("2018-06-16 02:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 03:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 04:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 05:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 06:00:00", formatter));

        to.add(LocalDateTime.parse("2018-06-18 02:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 03:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 04:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 05:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 06:00:00", formatter));

        for (int index = 0; index < from.size(); index++) {
            LocalDateTime fromDate = from.get(index);
            LocalDateTime toDate = to.get(index);
            String difference = formatDurationBetween(fromDate, toDate);
            System.out.println(fromDate.format(formatter) + " - " + toDate.format(formatter) + " = " + difference);
        }
    }

    public static String formatDurationBetween(LocalDateTime from, LocalDateTime to) {
        Duration difference = Duration.between(from, to);

        long days = difference.toDays();
        difference = difference.minusDays(days);
        long hours = difference.toHours();
        long mins = difference.minusHours(hours).toMinutes();

        return String.format("%dd %dh %02dm", days, hours, mins);
    }

}


输出...

2018-06-16 02:00:00 - 2018-06-18 02:00:00 = 2d 0h 00m
2018-06-16 03:00:00 - 2018-06-18 03:00:00 = 2d 0h 00m
2018-06-16 04:00:00 - 2018-06-18 04:00:00 = 2d 0h 00m
2018-06-16 05:00:00 - 2018-06-18 05:00:00 = 2d 0h 00m
2018-06-16 06:00:00 - 2018-06-18 06:00:00 = 2d 0h 00m


您当然可以根据自己的需要制作自己的格式化算法

答案 1 :(得分:3)

这就是你要求的,

LocalDateTime fromDate = LocalDateTime.of(2018, 6, 16, 2, 0, 0);
LocalDateTime toDate = LocalDateTime.of(2018, 6, 18, 2, 0, 0);
long minutes = Duration.between(fromDate, toDate).toMinutes();

<强>更新

根据以下评论,前导零被删除,因为它们容易出错。

答案 2 :(得分:2)

  

我需要输出只在分钟内。谢谢

已经有两个很好的答案,所以这只是为了补充一点,虽然Duration类是最通用和灵活的,用于表示两个日期时间之间的差异,但有一个更简单的选项,{{3} }:

    LocalDateTime fromdate = LocalDateTime.of(2018, Month.JUNE, 16, 2, 0, 0);
    LocalDateTime todate = LocalDateTime.of(2018, Month.JUNE, 18, 2, 0, 0);
    long diffInMinutes = ChronoUnit.MINUTES.between(fromdate, todate);
    System.out.println(diffInMinutes);

输出

  

2880