`

两日期之间的天数

    博客分类:
  • java
 
阅读更多
public int getBetweenDays(Date firstDate, Date nowDate){       
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");       
int betweenDays = 0;       
Calendar c1 = Calendar.getInstance();       
Calendar c2 = Calendar.getInstance();       
try {
c1.setTime(sdf.parse(sdf.format(firstDate)));       
c2.setTime(sdf.parse(sdf.format(nowDate)));       
// 保证第二个时间一定大于第一个时间       
if (c1.after(c2)) {       
    c1 = c2;       
    c2.setTime(sdf.parse(sdf.format(firstDate)));       
}
} catch (ParseException e) {
log.error(e);
}       
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;       
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics