两个DateTime对象之间的差异,以分钟为单位 [英] Difference between two DateTime objects in minutes

查看:67
本文介绍了两个DateTime对象之间的差异,以分钟为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

===========================================================
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 ,它仅计算时间。

I am using LocalDateTime but instead of getting the difference of 2 DateTime, it only calculated the time. It did not count the day.

这是代码:

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_的2个小时内等于120分钟减去2

The output of above code is zero since, in 2 hours in date_time_from equivalent to 120 mins subtracted to 2 hours in date_time_to.

还有另一种方法来获取总分钟数并包括计算日期吗?

Is there another way to get the total minutes and include to compute the date?

推荐答案

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);



然后,您可以使用类似于...的格式对其进行格式化。


Then you can format it using something similar to...

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);



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


And just to prove the point...

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);
    }

}



输出...


Outputs...

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



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


You can of course make your own formatting algorithm based on your needs

这篇关于两个DateTime对象之间的差异,以分钟为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆