Java 两个日期之间的天数比较->;更简单的方法?
嗨,我在问自己,是否有更简单的方法来计算两次约会之间的天数 我只想要几天,不看时间和分钟 因此,如果今天是星期一,我想比较的日期是星期三,那么两天之间的天数是2(时间无关紧要) 因此,我使用以下代码:Java 两个日期之间的天数比较->;更简单的方法?,java,date,comparison,calendar,Java,Date,Comparison,Calendar,嗨,我在问自己,是否有更简单的方法来计算两次约会之间的天数 我只想要几天,不看时间和分钟 因此,如果今天是星期一,我想比较的日期是星期三,那么两天之间的天数是2(时间无关紧要) 因此,我使用以下代码: Calendar c = Calendar.getInstance(); // Only the day: c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.s
Calendar c = Calendar.getInstance();
// Only the day:
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Calendar to = Calendar.getInstance();
to.setTime(date);
to.set(Calendar.HOUR, 0);
to.set(Calendar.MINUTE, 0);
to.set(Calendar.SECOND, 0);
to.set(Calendar.MILLISECOND, 0);
date = to.getTime();
long millsPerDay = 1000 * 60 * 60 * 24;
long dayDiff = ( date.getTime() - dateToday.getTime() ) / millsPerDay;
在这段代码之后,我有一个名为dayDiff的长时间段中的天。
但是,是否真的需要为日期制作日历,将时间设置为00:00:00,并将保存到日期中的.getTime()
编辑:使用joda时间后:
joda time是否也可以获取有关日期的信息,例如:
差异==1==>明天,或差异==-1==>昨天
还是必须手动执行此操作?您可以使用如图所示的API
很抱歉我的疏忽您可以使用commons lang,而不是将所有不相关的值设置为0
无论如何,dayDiff(start-end)/miliesperday将无法正常工作,因为日光保存更改。对于指定的任务,我总是使用这种方便的方式:(没有lib,只有Java5API)
享受吧 这是一种分析性daydiff方法,不基于危险毫秒转换:
public static int dayDiff(Calendar to, Calendar from){
int result = 0;
int years;
// global year difference from 1.jan to 1.jan
years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR);
result = years * 365;
// adding days for simple leap years ( divisible by 4 ). This an approximation that will be corrected by the negative leap years formula.
result += (to.get(Calendar.YEAR)-1)/4 - (from.get(Calendar.YEAR)-1)/4;
// removing days for negative leap years ( divisible by 100 ). This is still an approximation that will be corrected by the big leap years formula.
result -= (to.get(Calendar.YEAR)-1)/100 - (from.get(Calendar.YEAR)-1)/100;
// adding days for big leap years ( divisible by 400 ). After this formula, the days count from 1.jan.<from> to 1.jan.<to> is correct.
result += (to.get(Calendar.YEAR)-1)/400 - (from.get(Calendar.YEAR)-1)/400;
// adding month of to-year
for(int m=0; m<to.get(Calendar.MONTH ); m++){
result += daysInMonth(m, to.get(Calendar.YEAR));
}
// substracting month of from-year
for(int m=0; m<from.get(Calendar.MONTH ); m++){
result -= daysInMonth(m, from.get(Calendar.YEAR));
}
// adding days of to-year
result += to.get(Calendar.DAY_OF_MONTH );
// substracting days of from-year
result -= from.get(Calendar.DAY_OF_MONTH );
return result;
}
private static int daysInMonth(int m, int y){
if(m==3 || m==5 || m==8 || m==10) return 30;
if(m==1)
if(isLeapYear(y)) return 29;
else return 28;
return 31;
}
private static boolean isLeapYear(int y){
return (isSimpleLeapYear(y) && !isNegativeLeapYear(y)) || isBigLeapYear(y);
}
private static boolean isSimpleLeapYear(int y){
return y%4 == 0;
}
private static boolean isNegativeLeapYear(int y){
return y%100 == 0;
}
private static boolean isBigLeapYear(int y){
return y%400 == 0;
}
public static int dayDiff(日历到,日历从){
int结果=0;
整数年;
//1月1日至1月1日的全球年度差异
years=to.get(Calendar.YEAR)-from.get(Calendar.YEAR);
结果=年*365;
//为简单闰年加上天数(可被4整除)。这是一个近似值,将由负闰年公式修正。
结果+=(to.get(日历年)-1)/4-(from.get(日历年)-1)/4;
//删除负闰年的天数(可被100整除)。这仍然是一个近似值,将由大闰年公式进行修正。
结果-=(to.get(Calendar.YEAR)-1)/100-(from.get(Calendar.YEAR)-1)/100;
//为大跃进年添加天数(可被400整除)。根据此公式,从1.1月到1.1月的天数是正确的。
结果+=(to.get(日历年)-1)/400-(from.get(日历年)-1)/400;
//将月份添加到年度
对于(int m=0;mWhy您使用double
和float
常量?如果您使用long
您将不需要强制转换或调用floor
是的,我会使用long
并将其作为常量。请注意不要像最初那样被内联乘法除,因为它会被解释为int
>。它可以在一天内正常工作,但是如果你除以1000*60*60*24*30
一个月,它会溢出int
,你不会得到预期的结果。谢谢你们,我想总有一天我会获得愚蠢的诺贝尔奖。:)现在修复我的代码它也支持日光节约吗(如拉尔夫所说)就我个人而言,我更喜欢使用现有的日期库而不是自己的。日期处理有很多细微差别,很容易被忽略。
import java.util.concurrent.TimeUnit;
Date d1 = ...
Date d2 = ...
long daysBetween = TimeUnit.MILLISECONDS.toDays(d2.getTime() - d1.getTime());
public static int dayDiff(Calendar to, Calendar from){
int result = 0;
int years;
// global year difference from 1.jan to 1.jan
years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR);
result = years * 365;
// adding days for simple leap years ( divisible by 4 ). This an approximation that will be corrected by the negative leap years formula.
result += (to.get(Calendar.YEAR)-1)/4 - (from.get(Calendar.YEAR)-1)/4;
// removing days for negative leap years ( divisible by 100 ). This is still an approximation that will be corrected by the big leap years formula.
result -= (to.get(Calendar.YEAR)-1)/100 - (from.get(Calendar.YEAR)-1)/100;
// adding days for big leap years ( divisible by 400 ). After this formula, the days count from 1.jan.<from> to 1.jan.<to> is correct.
result += (to.get(Calendar.YEAR)-1)/400 - (from.get(Calendar.YEAR)-1)/400;
// adding month of to-year
for(int m=0; m<to.get(Calendar.MONTH ); m++){
result += daysInMonth(m, to.get(Calendar.YEAR));
}
// substracting month of from-year
for(int m=0; m<from.get(Calendar.MONTH ); m++){
result -= daysInMonth(m, from.get(Calendar.YEAR));
}
// adding days of to-year
result += to.get(Calendar.DAY_OF_MONTH );
// substracting days of from-year
result -= from.get(Calendar.DAY_OF_MONTH );
return result;
}
private static int daysInMonth(int m, int y){
if(m==3 || m==5 || m==8 || m==10) return 30;
if(m==1)
if(isLeapYear(y)) return 29;
else return 28;
return 31;
}
private static boolean isLeapYear(int y){
return (isSimpleLeapYear(y) && !isNegativeLeapYear(y)) || isBigLeapYear(y);
}
private static boolean isSimpleLeapYear(int y){
return y%4 == 0;
}
private static boolean isNegativeLeapYear(int y){
return y%100 == 0;
}
private static boolean isBigLeapYear(int y){
return y%400 == 0;
}