【问题标题】:How to get the the number of days between two dates - `java.util.Calendar` [duplicate]如何获取两个日期之间的天数 - `java.util.Calendar` [重复]
【发布时间】:2014-06-08 10:49:07
【问题描述】:

我正在尝试获取两个日期之间的天数。我在 JFXtras 的 Agenda 工作。

开始时间 = getStartTime() 返回java.util.Calendar
结束时间 = getEndTime() 返回java.util.Calendar

这是我目前得到的:

void printAppointment(Appointment a) throws ParseException {
        System.out.println(a.getSummary());
        System.out.println(a.getDescription());

        DateFormat formatter = new SimpleDateFormat("yyyy, MMM dd h:mm:ss a z");
        System.out.println(formatter.format(a.getStartTime().getTime()));
        System.out.println(formatter.format(a.getEndTime().getTime()));
        System.out.println(a.getAppointmentGroup());
        System.out.println(a.getLocation());

        // My problem starts here
        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");

        Date date = sdf.parse(formatter.format(a.getStartTime().getTime()));
        cal1.setTime(date);
        date = sdf.parse(formatter.format(a.getEndTime().getTime()));
        cal2.setTime(date);
        // System.out.println("Days difference = " + daysBetween(cal1.getTime(), cal2.getTime()));
        System.out.println("Days difference = can't be set");
    }

    private int daysBetween(Date d1, Date d2) {
        return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
    }

// My problem starts here 之后我什么也得不到。打印以下内容:

Another Morning appointment
Another Description of morning appointment
2014, Jun 12 6:00:00 AM PDT
2014, Jun 13 2:00:00 AM PDT
jfxtras.labs.scene.control.Agenda$AppointmentGroupImpl@16e4577
null

请注意,天数的差异会被忽略。

如果有人可以帮助我,我会非常感激。谢谢大家。

更新:

这是最终的工作实现,它以天、小时、分钟和秒为单位,如下面的尝试所示。

Pel
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget ti
2014. Jun Mon.9 3:18PM
2014. Jun Wed.11 3:18PM
jfxtras.labs.scene.control.Agenda$AppointmentGroupImpl@12e3872
null
diffSeconds difference = 172800
diffMinutes difference = 2880
diffHours difference = 48
diffDays difference = 2

这是执行此操作的代码

void printAppointment(Appointment a) {
    System.out.println(a.getSummary());
    System.out.println(a.getDescription());

    DateFormat formatter = new SimpleDateFormat("yyyy. MMM EEE.d h:mma");
    System.out.println(formatter.format(a.getStartTime().getTime()));
    System.out.println(formatter.format(a.getEndTime().getTime()));
    System.out.println(a.getAppointmentGroup());
    System.out.println(a.getLocation());
    this.daysBetween(a.getStartTime().getTimeInMillis(), a.getEndTime().getTimeInMillis());

}

private void daysBetween(long t1, long t2) {
    int diff = (int) (t2 - t1);

    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);

    System.out.println("diffSeconds difference = " + diffSeconds);
    System.out.println("diffMinutes difference = " + diffMinutes);
    System.out.println("diffHours difference = " + diffHours);
    System.out.println("diffDays difference = " + diffDays);
}

【问题讨论】:

标签: java calendar jfxtras


【解决方案1】:

不要将 Date 传递给 daysBetween,而是传递一个 long。您可以使用 getTimeInMillis() 从日历中获取当前时间(自 1970 年 1 月 1 日起)。您的代码现在如下所示:

void printAppointment(Appointment a) throws ParseException {
    System.out.println(a.getSummary());
    System.out.println(a.getDescription());

    System.out.println(formatter.format(a.getStartTime().getTime()));
    System.out.println(formatter.format(a.getEndTime().getTime()));
    System.out.println(a.getAppointmentGroup());
    System.out.println(a.getLocation());


    System.out.println("Days difference = " + daysBetween(a.getStartTime().getTimeInMillis(), a.getEndTime().getTimeInMillis()));
}

private int daysBetween(long t1, long t2) {
    return (int) ((t2 - t1) / (1000 * 60 * 60 * 24));
} 

【讨论】:

  • 感谢@Simon 的帮助。我试过了,它可以用好几天。我试图获得小时、分钟和秒的差异,但无法获得。能否请您看一下我上面的更新,如果您能帮忙吗?
  • 您的代码对我有用。您遇到什么问题? The code I'm using。 PS计算可以通过使用来简化:long diffSeconds = diff / 1000;长 diffMinutes = diffSeconds / 60;长 diffHours = diffMinutes / 60; long diffDays = diffHours / 24;
  • 对不起!做了清理和构建,刷新了项目。我认为我的 IDE 没有接受新的代码编辑。谢谢你一百万。
  • 愚蠢的 IDE。不客气!
  • @stuart,总是有更多的东西你可以edit with a post,所以一个字符的编辑不会削减它。请不要将编辑摘要放在帖子中,这就是编辑摘要的用途。
【解决方案2】:

如果您被允许使用 Java 8,那么您应该查看 https://stackoverflow.com/users/288671/vitalii-fedorenko 对问题 Difference in days between two dates in Java? 的回答

在 Java 8 中引入新 API 有几个原因 - 正确处理时差是其中一个原因。 (在某些时区可能会发生影响计算的变化 - 因此,如果有白天调整或类似的事情,仅除以毫秒为单位的差异会导致意外结果)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多