我想要在java中的两个日期之间的区别?

时间:2012-12-10 09:31:04

标签: java

  

可能重复:
  Calculating the Difference Between Two Java Date Instances
  how to calculate difference between two dates using java

我试图通过一些例子来区分两个日期,但我没有得到正确答案。

java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
 java.util.Date date1 = df.parse("2012-09-14 15:26:14+00");
 java.util.Date date2 = df.parse("2012-08-30 15:26:14+00");
 long diff = Math.abs(date2.getTime() - date1.getTime());
 System.out.println("millisecond="+diff);

我也希望月份差异,但它没有给我。它给了我不同的日,小时,分钟和秒,但不是月份。我做错了什么?请帮帮我

编辑:

java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
 java.util.Date date1 = df.parse("2012-09-30 15:26:14+00");
 java.util.Date date2 = df.parse("2012-08-30 15:26:14+00");
 long diff = Math.abs(date2.getTime() - date1.getTime());
 System.out.println(date2.getTime()+"date2");
 System.out.println(date1.getTime()+"date1");
 System.out.println("millisecond="+diff);
 diff=diff/1000;
 System.out.println("second="+diff);
 long days=diff/86400;
 days=days%86400;
 long hours=diff/3600;
 hours=hours%3600;
 long min=diff/60;
 min=min%60;
 hours=(hours-(24*days));
 String time=days+":"+hours+":"+min;
 System.out.println(time);
System.out.println("days="+days+":"+"hours="+hours+":"+"minutes"+min);

这就是我想要做的。

5 个答案:

答案 0 :(得分:5)

您可以将Joda时间库用于Java。使用它来计算日期之间的时差可能会容易得多。

Sample snippet for time-diff

Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();

答案 1 :(得分:2)

将其称为getDuration(date1,date2,Calendar.MONTH);

 public static long getDuration(Date returnTime, Date leaveTime, int scale) {
        long durationInMillis = returnTime.getTime() - leaveTime.getTime();
        switch (scale) {
            case Calendar.MINUTE:
                return durationInMillis / ONE_MINUTE_IN_MILLIS;
            case Calendar.MILLISECOND:
                return durationInMillis;
            case Calendar.SECOND:
                return durationInMillis / ONE_SECOND_IN_MILLIS;
            case Calendar.HOUR:
                return durationInMillis / ONE_HOUR_IN_MILLIS;
            case Calendar.DAY_OF_YEAR:
            case Calendar.DATE:
                return durationInMillis / ONE_DAY_IN_MILLIS;
            case Calendar.MONTH:
                return durationInMillis / ONE_MONTH_IN_MILLIS; // 30days per month
        }
        throw new IllegalArgumentException("invalid scale specified");
    }

答案 2 :(得分:1)

Date.getTime返回毫秒,而不是秒。你需要除以1000得到秒。

long diff = Math.abs(date2.getTime() - date1.getTime()) / 1000;

同样如评论中所述,您的格式字符串不正确。它应该是这样的:

"yyyy-MM-dd HH:mm:ss"

答案 3 :(得分:0)

你得到的差异是两个日期之间的毫秒的数量,而不是秒。请注意,您提供的日期之间的差异不是整整一个月。

答案 4 :(得分:0)

首先让我们改变yyyy-mm-dd hh:mm:ss到yyyy-MM-dd HH:mm:ss。

日期之间的差异只能用Calendar类来计算,而下面是我的基于日历的解决方案,不确定它是最好的解决方案,但它肯定是在做它的工作。它计算填充月份的日期差异,即2012-09-30 15:26:14 + 00 - 2012-08-30 15:26:14 + 00是1个月。但是2012-09-30 15:26:14 + 00 - 2012-08-30 15:26:15 + 00是0个月。 请注意,Locale也很重要,因为结果取决于节电设置。 我不会评论它希望代码中的一切都清楚

public static void main(String[] args) throws Exception {
    java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date date1 = df.parse("2012-09-30 15:26:14+00");
    java.util.Date date2 = df.parse("2012-08-30 15:26:14+00");
    int diff = getMonthDifference(date1, date2);
    System.out.println(diff);
}

public static int getMonthDifference(java.util.Date date1, java.util.Date date2) {
    if (date1.after(date2)) {
        return getMonthDifference0(date1, date2);
    } else if (date2.after(date1)) {
        return -getMonthDifference0(date2, date1);
    }
    return 0;
}

private static int getMonthDifference0(java.util.Date date1, java.util.Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        int diff = 0;
        while (c2.getTimeInMillis() < c1.getTimeInMillis()) {
            c2.add(Calendar.MONTH, 1);
            diff++;
        }
        int dd = c2.get(Calendar.DAY_OF_MONTH) - c1.get(Calendar.DAY_OF_MONTH);
        if (dd > 0) {
            diff--;
        } else if (dd == 0) {
            int hd = c2.get(Calendar.HOUR_OF_DAY) - c1.get(Calendar.HOUR_OF_DAY);
            if (hd > 0) {
                diff++;
            } else if (hd == 0) {
                long t1 = c1.getTimeInMillis() % (60 * 1000);
                long t2 = c2.getTimeInMillis() % (60 * 1000);
                if (t2 > t1) {
                    diff--;
                }
            }
        }
        return diff;
    }