首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

两日期其间的天数

2012-10-23 
两日期之间的天数public int getBetweenDays(Date firstDate, Date nowDate){SimpleDateFormat sdf new

两日期之间的天数
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;       
}

热点排行