import java.util.Scanner;

public class DateUtil { private int year, month, day;

public DateUtil(int year, int month, int day) {
    this.year = year;
    this.month = month;
    this.day = 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 boolean checkInputValidity() {
    if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31) {
        return false;
    }
    return true;
}

public boolean isLeapYear(int year) {
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
        return true;
    }
    return false;
}

public DateUtil getNextNDays(int n) {
    int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int newDay = day + n;
    int newMonth = month;
    int newYear = year;

    if (isLeapYear(year)) {
        daysInMonth[1] = 29;
    }

    while (newDay > daysInMonth[newMonth - 1]) {
        newDay -= daysInMonth[newMonth - 1];
        newMonth++;
        if (newMonth > 12) {
            newMonth = 1;
            newYear++;
            if (isLeapYear(newYear)) {
                daysInMonth[1] = 29;
            } else {
                daysInMonth[1] = 28;
            }
        }
    }

    return new DateUtil(newYear, newMonth, newDay);
}

public DateUtil getPreviousNDays(int n) {
    int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int newDay = day - n;
    int newMonth = month;
    int newYear = year;

    if (isLeapYear(year)) {
        daysInMonth[1] = 29;
    }

    while (newDay < 1) {
        newMonth--;
        if (newMonth < 1) {
            newMonth = 12;
            newYear--;
            if (isLeapYear(newYear)) {
                daysInMonth[1] = 29;
            } else {
                daysInMonth[1] = 28;
            }
        }
        newDay += daysInMonth[newMonth - 1];
    }

    return new DateUtil(newYear, newMonth, newDay);
}

public boolean compareDates(DateUtil date) {
    if (year < date.getYear()) {
        return true;
    } else if (year == date.getYear() && month < date.getMonth()) {
        return true;
    } else if (year == date.getYear() && month == date.getMonth() && day < date.getDay()) {
        return true;
    }
    return false;
}

public boolean equalTwoDates(DateUtil date) {
    if (year == date.getYear() && month == date.getMonth() && day == date.getDay()) {
        return true;
    }
    return false;
}

public int getDaysofDates(DateUtil date) {
    int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int days = 0;

    if (year == date.getYear() && month == date.getMonth() && day == date.getDay()) {
        return days;
    }

    int startYear = year;
    int startMonth = month;
    int startDay = day;

    int endYear = date.getYear();
    int endMonth = date.getMonth();
    int endDay = date.getDay();

    while (startYear != endYear || startMonth != endMonth || startDay != endDay) {
        days++;
        startDay++;
        if (startDay > daysInMonth[startMonth - 1]) {
            startDay = 1;
            startMonth++;
            if (startMonth > 12) {
                startMonth = 1;
                startYear++;
                if (isLeapYear(startYear)) {
                    daysInMonth[1] = 29;
                } else {
                    daysInMonth[1] = 28;
                }
            }
        }
    }

    return days;
}

public String showDate() {
    return year + "-" + month + "-" + day;
}

}

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

原文地址: https://www.cveoy.top/t/topic/o638 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录