获取两个日期之间相差的天数
/**
* 计算两个日期之间相差的天数.
* @param startDate 开始日期
* @param endDate 结束日期
* @return 天数
*/
public int daysBetween(Date startDate, Date endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
endDate = sdf.parse(sdf.format(endDate));
startDate = sdf.parse(sdf.format(startDate));
} catch (Exception e) {
log.error("日期转换异常:", e);
}
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
long time1 = cal.getTimeInMillis();
cal.setTime(startDate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Math.abs(Integer.parseInt(String.valueOf(between_days)));
}
本文标题:获取两个日期之间相差的天数
本文链接:https://www.haomeiwen.com/subject/jwzoultx.html
网友评论