获取两个日期相差天数

2018-05-09  本文已影响0人  小知大知

//获取两个日期相差天数

public int getDiscrepantDays(Date dateStart, Date dateEnd)  {
    //先将小时,分,秒设置为零,防止转换造成一天的误差
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(dateStart);
    calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
    Date date1 = calendar.getTime();
    calendar.setTime(dateEnd);
    calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
    Date date2 =  calendar.getTime();
    long day = (date2.getTime() - date1.getTime()) / (24*3600*1000);
    return (int)day+1;
}

上一篇下一篇

猜你喜欢

热点阅读