gpt4 book ai didi

java - 在java中查找天数差异

转载 作者:行者123 更新时间:2023-11-29 07:10:32 25 4
gpt4 key购买 nike

在咨询了几个论坛之后,我最终使用下面的代码来查找天差。但是,我发现逻辑有问题(可能是我的疏忽?)。我看到 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

最佳答案

使用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);
}

关于java - 在java中查找天数差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14269495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com