【问题标题】:Wrong time calculated in minutes from two Date objects to calculate duration从两个 Date 对象计算持续时间的错误时间(以分钟为单位)
【发布时间】:2013-07-06 02:15:04
【问题描述】:

我试图以分钟为单位计算两个日期对象之间的持续时间。

我在研究过程中从thisstackoverflow 问题中找到了一些灵​​感。一般来说,这似乎是正确的,但我在一个测试用例中遇到了一种有趣的行为。

当我运行下面附加的源代码(您可以简单地复制过去)时,它返回了 66 分钟而不是(正确的结果)6,我目前不明白为什么。也许我现在正在监督一些事情,你能告诉我它是什么吗?

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Test {

    private SimpleDateFormat parserSDF = new SimpleDateFormat("MM/dd/yy HH:mm",
            Locale.ENGLISH);

    public static void main(String[] args) {
        Test test = new Test();
        Date begin = test.createDateFromString("10/25/09 1:54");
        Date end = test.createDateFromString("10/25/09 2:00");

        int duration = test.minutesDiff(begin, end);
        //result is 66
        System.out.println(duration);
    }

    public int minutesDiff(Date earlierDate, Date laterDate) {
        if (earlierDate == null || laterDate == null)
            return 0;

        return (int) ((laterDate.getTime() / 60000) - (earlierDate.getTime() / 60000));
    }

    public Date createDateFromString(String dateString) {
        Date date = null;
        try {
            date = parserSDF.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

我知道顺便说一句,存在这个 Joda 库,它在计算这些东西方面做得更好,但如果可能的话,我希望不使用外部库。

感谢您与我分享的每一个想法。

编辑:啊啊,这可能是因为时钟变化吗?时钟更改是在 2009 年 10 月 25 日,时间从凌晨 3 点调回到凌晨 2 点。这可能意味着这个结果是正确的

【问题讨论】:

标签: java date datetime time duration


【解决方案1】:
 laterDate.getTime()  

以毫秒为单位返回 long 值。您正在将 long 值转换为 int。这将导致不正确的时间延迟。

【讨论】:

  • 他在计算结束时转换为 int,而不是针对每个 getTime()。无论最后是否转换为 int,最终结果都应为 6
  • @giorashc 我说这可能会导致不正确的时差。不是每次都
  • 我严重怀疑答案中的 MAY :)。
  • 在演员表之前,OP 将毫秒转换为分钟。只有当分钟差 > Integer.MAX_VALUE(大约 4085 年——在这种情况下不太可能出现)时,铸造才会丢失信息。
【解决方案2】:

根据您所在的地区,它看起来像是夏令时(也称为夏令时)的结束。 Here's the 2009 table; 2009 年 10 月 25 日是许多地区的结束日期。这将解释为什么出现 66 而不是 6;还有 60 分钟。

【讨论】:

  • 是的,我也刚刚发现了这个 :) 我认为这就是原因
  • 鉴于 60 分钟的错误,这是我要调查的第一件事。
  • 愚蠢的我,我之前没想到这一点
  • @Waylander 看起来我们几乎同时发现了错误。
【解决方案3】:

java.util 的日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,转用modern date-time API

演示:

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/uu H:m", Locale.ENGLISH);
        ZoneId zoneId = ZoneId.of("America/New_York");// Change it the required timezone
        ZonedDateTime begin = LocalDateTime.parse("10/25/09 1:54", dtf).atZone(zoneId);
        ZonedDateTime end = LocalDateTime.parse("10/25/09 2:00", dtf).atZone(zoneId);
        long minutes = Duration.between(begin, end).toMinutes();
        System.out.println(minutes);
    }
}

输出:

6

Trail: Date Time 了解现代日期时间 API。

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 2019-03-22
    • 2011-04-19
    • 1970-01-01
    • 2018-03-28
    • 2021-05-12
    • 2011-11-29
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多