日期计算

相差天数:

    public static int getDayDiff(Date startTime, Date endTime){
        return (int) ((endTime.getTime() + 28800000) / 86400000 - (startTime.getTime() + 28800000) / 86400000);
    }

相差周数:

    public static int getWeekDiff(Date startTime, Date endTime){
        return (int) ((endTime.getTime() + 288000000) / 604800000 - (startTime.getTime() + 288000000) / 604800000);
    }

相差月数:

    public static int getMonthDiff(Date startTime, Date endTime) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startTime);
        end.setTime(endTime);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

 

posted @ 2019-03-19 17:59  不知为何就叫呵呵  阅读(374)  评论(0编辑  收藏  举报