【问题标题】:Difference between two DateTime objects in minutes两个 DateTime 对象之间的差异(以分钟为单位)
【发布时间】:2018-11-25 19:31:47
【问题描述】:
===========================================================
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 中的 2 小时等于 date_time_to 中的 2 小时减去 120 分钟。

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

【问题讨论】:

标签: java datetime localdate


【解决方案1】:
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


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

【讨论】:

  • 好的,那么,根据上面的内容,你认为你可能需要改变什么(提示DurationtoMinutes,但上面的例子是“修改”Duration )?
【解决方案2】:

这是你要求的吗,

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

更新

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

【讨论】:

  • 这个答案似乎很中肯,因为需要几分钟。顺便说一句,虽然 00 用于指定 0 分钟和 0 秒,但一旦尝试 08 8 分钟或 09 9 秒,您就会遇到麻烦,因为 Java 将带有前缀 0 的数字视为 @ 987654321@。所以最好避免这种情况(当然,除非你有理由使用八进制数)。
  • @OleV.V.是的,因为上帝禁止我们让人们自己思考或尽职尽责地解决他们的问题?
【解决方案3】:

我只需要几分钟的输出。谢谢

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

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多