Java 日期近1年、几月、几天

阅读更多
一個月
Date date1 = new Date();
        int year=date1.getYear();
        int month=date1.getMonth();
        int day=date1.getDate();
        if (month == 0) {
            date1.setYear(year- 1);
            date1.setMonth(11);
        } else {
            if(month==2 && day>28){
                date1.setMonth(month-1);
                date1.setDate(28);
            }else if(month == 4 || month == 6 || month==7 || month == 9 || month == 11){
                date1.setMonth(month - 1);
                if(day>30){
                    date1.setDate(30);
                }
            }else{
                date1.setMonth(month - 1);
            }
           
        }
三個月
        int year=date1.getYear();
        int month=date1.getMonth();
        int day=date1.getDate();
        if(month>=3){
            if(month==4 && day>28){
                date1.setDate(28);
                date1.setMonth(month-3);
            }else if( (month == 6 || month == 11) && day>30){
                date1.setDate(30);
                date1.setMonth(month-3);
            }else{
                date1.setMonth(month-3);
            }
        }else{
            if(month==1){
                if(day>30){
                    date1.setDate(30);
                }
            }
                date1.setMonth(9+month);
                date1.setYear(year-1);
        }
半年
        int year=date1.getYear();
        int month=date1.getMonth();
        int day=date1.getDate();
        if(month>=6){
            if(month==7 && day>28){
                date1.setDate(28);
                date1.setMonth(month-6);
            }else if( (month == 9 || month == 11) && day>30){
                date1.setDate(30);
                date1.setMonth(month-6);
            }else{
                date1.setMonth(month-6);
            }
        }else{
            if(month==2 || month==4){
                if(day>30){
                    date1.setDate(30);
                }
            }
                date1.setMonth(6+month);
                date1.setYear(year-1);
        }
七天
        int year=date1.getYear();
        int month=date1.getMonth();
        int day=date1.getDate();

        if (day > 7) {
            date1.setDate(day-7);
        } else {
            if (month == 0) {
                date1.setYear(year-1);
                date1.setMonth(11);
                date1.setDate(24+day);
            } else if (month==1 || month == 3 || month == 5 || month==7 || month == 8 || month == 10) {
                date1.setMonth(month-1);
                date1.setDate(24+day);
            } else if (month == 2) {
                if (year % 4 == 0) {
                date1.setMonth(month-1);
                date1.setDate(22+day);
                } else {
                date1.setMonth(month-1);
                date1.setDate(21+day);
                }
            } else {
                date1.setMonth(month-1);
                date1.setDate(23+day);
            }
        }

三天
        if (day > 3) {
            date1.setDate(day-3);
        } else {
            if (month == 0) {
                date1.setYear(year-1);
                date1.setMonth(11);
                date1.setDate(28+day);
            } else if (month==1 || month == 3 || month == 5 || month==7 || month == 8 || month == 10) {
                date1.setMonth(month-1);
                date1.setDate(28+day);
            } else if (month == 2) {
                if (year % 4 == 0) {
                date1.setMonth(month-1);
                date1.setDate(26+day);
                } else {
                date1.setMonth(month-1);
                date1.setDate(25+day);
                }
            } else {
                date1.setMonth(month-1);
                date1.setDate(27+day);
            }
        }

一年

        if(date.getMonth()==1 && date.getDate()==29){
            date.setDate(28);
            date.setYear(date.getYear()-1);
        }else{
        date.setYear(date.getYear() - 1);
        }



你可能感兴趣的:(Java 日期近1年、几月、几天)