
jadeutils.base.DateCaculater Maven / Gradle / Ivy
The newest version!
package jadeutils.base;
import java.util.Calendar;
import java.util.Date;
public class DateCaculater {
/*
* 取得两个时间段的时间间隔 return t2 与t1的间隔天数 throws ParseException
* 如果输入的日期格式不是0000-00-00 格式抛出异常
*/
public static int getBetweenDays(Date d1, Date d2) {
int betweenDays = 0;
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
// 保证第二个时间一定大于第一个时间
if (c1.after(c2)) {
c1 = c2;
c2.setTime(d1);
}
int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
betweenDays = c2.get(Calendar.DAY_OF_YEAR)
- c1.get(Calendar.DAY_OF_YEAR);
for (int i = 0; i < betweenYears; i++) {
c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
}
return betweenDays;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy