JAVA DATE 计算相隔天数+日期增加减少

纵饮孤独 提交于 2019-12-26 12:39:41
    //计算两日期相差天数
    public int testBetweenDays(Date opDate,Date enDate) {
        // 获取相差的天数
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(opDate);
        long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(enDate);
        long timeInMillis2 = calendar.getTimeInMillis();

        int betweenDays = (int) ((timeInMillis2 - timeInMillis1) / (1000L * 3600L * 24L));
        return betweenDays;
    }

    public  Date addDayOfDate(Date date,int i){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, i);
        Date newDate = c.getTime();
        return newDate;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!