面向对象的第三次pta作业第二题:日期类设计

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() + 
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}
class DateUtil {
    int year;
    int month;
    int day;
    
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public DateUtil(){}
    
    public DateUtil(int year, int month, int day){
        this.year = year;
        this.day = day;
        this.month = month;
    }
    public boolean checkInputValidity(){//检测输入的年、月、日是否合法
        if(year>2020||year<1820||month>12||month<1||day<1||day>31) {
//            System.out.println("Wrong Format");
            return false;
        }
        switch (month) {
        case 4:case 6:case 9:case 11:
            if (day>30) {
//                System.out.println("Wrong Format");
                return false;
            }
            break;
        case 2:
            if (isLeapYear(year)&&day>29) {
//                System.out.println("Wrong Format");
                return false;
            }
            if (day>28) {
//                System.out.println("Wrong Format");
                return false;
            }
            break;
        }
        return true;
    }
    public boolean isLeapYear(int year){//判断year是否为闰年
        boolean y1 = year%4 == 0;
        boolean y2 = year%100 != 0;
        boolean y3 = year%400 == 0;
        
        if((y1&&y2)||y3)
            return true;
        else 
            return false;
    }
    
    public DateUtil getNextNDays(int n){//取得year-month-day的下n天日期
        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int year=0, month=0, day=0;
        int rest = restday(this);
        if (rest>n) {//本年
            year=this.year;
            int mday = arr[this.month];
            if (isLeapYear(this.year)&&this.month==2) {
                mday++;
            }
            mday-=this.day;//本月剩余的日期
            if (mday>=n) {    //本月
                month = this.month;
                day = n+this.day;
            }
            else{    //其他月
                n-=mday;
                month = this.month+1;
                int k = month;
                while(n-arr[k]>0&&k<=12){
                    n -= arr[k];
                    month++;
                    k++;
                }
                day = n;
            }
        }
        else {
            n-=rest;
            year = this.year+1;
            int y = 365;
            if (isLeapYear(year)) {
                y++;
            }
            while(n-y>0){
                n-=y;
                year++;
                y=365;
                if (isLeapYear(year))
                    y++;
            }
            int k = 1;
            while(n-arr[k]>0&&k<=12){
                n -= arr[k];
                k++;
            }
            month = k;
            day = n;
        }
        
//        System.out.println(this.showDate()+" next "+n+" days is:"+year+"-"+month+"-"+day);
        
        return new DateUtil(year, month, day);
    }
    
    public int restday(DateUtil d) {
        int n = 0;
        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        for (int i = d.month+1; i <=12; i++) {
            n+=arr[i];
        }
        n+=arr[d.month]-d.day;
        if(isLeapYear(d.year)&&d.month<=2)
            n++;
        return n;
    }
    
    public DateUtil getPreviousNDays(int n){//取得year-month-day的前n天日期
        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int year=0, month=0, day=0;
        int rest = 365-restday(this);
        if (isLeapYear(this.year)) {
            rest++;
        }
        if (rest>n) {//本年
            year=this.year;
            int mday=this.day;//本月剩余的日期
            if (mday>n) {    //本月
                month = this.month;
                day = mday-n;
            }
            else{    //其他月
                n-=mday;
                month = this.month-1;
                if (month==0) {
                    month = 12;
                    year=this.year-1;
                }
                int k = month;
                while(n-arr[k]>0&&k>=0){
                    n -= arr[k];
                    month--;
                    k--;
                }
                day = arr[k]-n;
                if (isLeapYear(year)&&month==2) {
                    day++;
                }
            }
        }
        else {
            n-=rest;
            year = this.year-1;
            int y = 365;
            if (isLeapYear(year)) {
                y++;
            }
            while(n-y>0){
                n-=y;
                year--;
                y=365;
                if (isLeapYear(year))
                    y++;
            }
            int k = 12;
            while(n-arr[k]>0&&k>=0){
                n -= arr[k];
                k--;
            }
            month = k;
            day = arr[k]-n;
            if (isLeapYear(year)&&month==2) {
                day++;
            }
        }
//        System.out.println(this.showDate()+" previous "+n+" days is:"+year+"-"+month+"-"+day);
        return new DateUtil(year, month, day);
    }
    public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后)
        if (date.year<this.year) {
            return false;
        }
        else if (date.year==this.year&&date.month<this.month) {
            return false;
        }
        if (date.year==this.year&&date.month==this.month&&date.day<this.day) {
            return false;
        }
        return true;
    }
    public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等
        if (this.day==date.day && this.year==date.year && this.month==date.month) {
            return true;
        }
        return false;
    }
    public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数
        DateUtil pred = this;
        DateUtil nextd = date;
        if (this.equalTwoDates(date)) {
            return 0;
        }
        else if (!this.compareDates(date)) {
            pred = date;
            nextd = this;
        }
        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int i,j,d = 0;
        for(i=pred.year+1;i<nextd.year;i++) {
            d=d+365;
            if(isLeapYear(i)) 
                d++;
        }
        if (pred.year!=nextd.year) {
            for(j=pred.month+1;j<=12;j++) 
                d=d+arr[j];
            d+=arr[pred.month]-pred.day;
            for(j=1;j<nextd.month;j++)
                d+=arr[j];
            d+=nextd.day;
            if(isLeapYear(pred.year)&&pred.month<=2)
                d++;
            if (isLeapYear(nextd.year)&&nextd.month>2) {
                d++;
            }
        }
        else if(pred.year==nextd.year&&pred.month!=nextd.month){
            for(j=pred.month+1;j<=nextd.month-1;j++)
                d+=arr[j];
            d+=arr[pred.month]-pred.day;
            d+=nextd.day;
            if(isLeapYear(pred.year)&&pred.month<=2)
                d++;
        }
        else if(pred.year==nextd.year&&pred.month==nextd.month){
            d=nextd.day-pred.day;
        }
        return d;
    }
    public String showDate(){//以“year-month-day”格式返回日期值
        return this.year+"-"+this.month+"-"+this.day;
    }
}

 

 

日期程序的4.0版本来咯!

开始构造属性了呢(虽然还是不大懂= =

posted on 2020-04-08 21:11  xyx's  阅读(998)  评论(1编辑  收藏  举报

导航