在java中查找天差异

时间:2013-01-11 00:00:13

标签: java date

在咨询了几个论坛之后,我最终使用下面的代码找到了天差。但是,我发现逻辑存在问题(可能是我的视线?)。我看到11至14和11至15之间的天数相同。怎么可能?

Date createdDate = new Date((2013 + 1900), (1 + 1), 11);
Date expirationDate = new Date((2013 + 1900), (1 + 1), 11);
for (int i = 11; i < 20; i++) {
    expirationDate.setDate(i);

    System.out.println("11 to " + i + " = "
            + (int) (expirationDate.getTime() - createdDate.getTime())
            / (1000 * 60 * 60 * 24));
}

输出结果为:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 3
11 to 16 = 4
11 to 17 = 5
11 to 18 = 6
11 to 19 = 7

5 个答案:

答案 0 :(得分:4)

使用Joda TimeDays#daysBetween()。没有更好的方法。

DateMidnight createdDate = new DateMidnight(2013, 2, 11);

for (int i = 11; i < 20; i++) {

    DateMidnight expirationDate = new DateMidnight(2013, 2, i);
    int dayDifference = Days.daysBetween(createdDate, expirationDate);

    System.out.println("11 to " + i + " = " + dayDifference);
}

答案 1 :(得分:1)

不推荐使用

Date(year,day, month )构造函数。我只想使用日历方法来获得两天之间的差异:

Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(createdDate);
cal2.setTime(expirationDate);
System.out.println(cal2.get(Calendar.DAY_OF_MONTH )-cal1.get(Calendar.DAY_OF_MONTH ) );

修改

Calendar cal1 = Calendar.getInstance();
cal1.set(2013, 2, 11);
Calendar cal2 = Calendar.getInstance();
cal2.set(2013, 2, 11);
for (int i = 11; i < 20; i++) {
 cal2.set(Calendar.DATE, i);
    System.out.println("11 to " + i + " = " + (cal2.get(Calendar.DAY_OF_MONTH) -cal1.get(Calendar.DAY_OF_MONTH)));
}

输出:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8

答案 2 :(得分:1)

1000 * 60 * 60 * 24是查找日差的错误方法。您可以使用JodaTime,但有一个纯java解决方案。

我们有两个初始化变量

Calendar firstDay = ...;
Calendar secondDay = ...;

firstDay.before(lastDay)

true

运行

int firstDayNo = firstDay.get(Calendar.DAY_OF_YEAR);
int secondDayNo = secondDay.get(Calendar.DAY_OF_YEAR);
int dayDifference, yearMultiplier;

dayDifference = -firstDayNo;
yearMultiplier = secondDay.get(Calendar.YEAR) - firstDay.get(Calendar.YEAR);
while (yearMultiplier > 0) {
    dayDifference += firstDay.getActualMaximum(Calendar.DAY_OF_YEAR);
    firstDay.add(Calendar.YEAR, 1);
    yearMultiplier--;
}
dayDifference += secondDayNo;

return dayDifference;

答案 3 :(得分:0)

使用float,我看到了问题。使用时间戳似乎不是找到2个日期之间的天差的好方法。

11到11 = 0.0
11至12 = 1.0
11至13 = 2.0
11至14 = 3.0
11至15 = 3.9583333
11至16 = 4.9583335
11至17 = 5.9583335
11至18 = 6.9583335
11至19 = 7.9583335

展望未来,我找到了确定日期差异的最具决定性的方法

Calendar cre_calendar = new GregorianCalendar((2013), (1), 11);
        Calendar exp_calendar = new GregorianCalendar((2013), (1), 19);
        Calendar maxDays = new GregorianCalendar(((2013)), (12), 31);

        if (exp_calendar.get(Calendar.DAY_OF_YEAR) < cre_calendar
                .get(Calendar.DAY_OF_YEAR)) {
            System.out
                    .println((exp_calendar.get(Calendar.DAY_OF_YEAR) + maxDays
                            .get(Calendar.DAY_OF_YEAR))
                            - cre_calendar.get(Calendar.DAY_OF_YEAR));
        } else {
            System.out.println((exp_calendar.get(Calendar.DAY_OF_YEAR))
                    - cre_calendar.get(Calendar.DAY_OF_YEAR));
        }

答案 4 :(得分:0)

至少在当前版本中,您的代码会打印正确的结果

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 4
11 to 16 = 5
11 to 17 = 6
11 to 18 = 7
11 to 19 = 8

尽管如此new Date((2013 + 1900), (1 + 1), 11);不正确,实际上它是5813-03-01。它应该是new Date((2013 - 1900), (1 - 1), 11);看日期(int year,int month,int day)API

Parameters:
year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.