java计算两个日期相差多少天

java计算两个日期相差多少天

public class DateUtil{
public static int betweenDays(Date startDate, Date endDate ) throws Exception {
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance() ; try {
beginCalendar.setTime( startDate ) ;
endCalendar.setTime( endDate ) ; if (beginCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR)) {
return endCalendar.get(Calendar.DAY_OF_YEAR)
- beginCalendar.get(Calendar.DAY_OF_YEAR);
} else {
if (beginCalendar.getTimeInMillis() < endCalendar.getTimeInMillis()) {
int days = beginCalendar.getActualMaximum(Calendar.DAY_OF_YEAR)
- beginCalendar.get(Calendar.DAY_OF_YEAR)
+ endCalendar.get(Calendar.DAY_OF_YEAR);
for (int i = beginCalendar.get(Calendar.YEAR) + 1; i < endCalendar
.get(Calendar.YEAR); i++) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, i);
days += c.getActualMaximum(Calendar.DAY_OF_YEAR);
}
return days;
} else {
int days =endCalendar.getActualMaximum(Calendar.DAY_OF_YEAR)
- endCalendar.get(Calendar.DAY_OF_YEAR)
+ beginCalendar.get(Calendar.DAY_OF_YEAR);
for (int i = endCalendar.get(Calendar.YEAR) + 1; i < beginCalendar
.get(Calendar.YEAR); i++) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, i);
days += c.getActualMaximum(Calendar.DAY_OF_YEAR);
}
return days;
}
}
} catch (Exception e) {
throw e;
}
} }
上一篇:可提高工作效率的 PL/SQL Developer 设置


下一篇:【转】Python3使用Django2.x的settings文件详解