日期計算

不知為何就叫呵呵發表於2019-03-19

相差天數:

    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);
    }

 

相關文章