简体   繁体   English

如何在公历中固定日期

[英]How to fix date in Gregorian Calendar

I want to fix the date to month/day/year in my class (Gregorian Calendar) so it works with my tester program.我想在我的 class (公历)中将日期固定为月/日/年,以便它与我的测试程序一起使用。 I get errors when running my tester program, I think it has to do with my string toString() method but I have tried to fix it but keep getting errors.运行我的测试程序时出现错误,我认为这与我的字符串 toString() 方法有关,但我已尝试修复它但不断出现错误。 I do not understand how having my string output to month + day + year would not work correctly in outputting mmmm/dd/yyyy.我不明白如何让我的字符串 output 到月 + 日 + 年在输出 mmmm/dd/yyyy 时无法正常工作。 Thank you for your help.谢谢您的帮助。

Errors:错误:

Exception in thread "main" java.lang.IllegalArgumentException
at Date.<init>(Date.java:15)
at Assign8B.main(Assign8B.java:14)

Class Class

public class Date {
    private int day, month, year;
    public Date() {
        this.day = 1;
        this.month = 1;
        this.year = 1970;
    }
    public Date(int year, int month, int day) {
        if (year < 1582) {
            throw new IllegalArgumentException();
        } else if (month <= 0 && month > 12) {
            throw new IllegalArgumentException();
        } else if (!isLeapYear(year) && (month == 2 && day == 29)) {
            throw new IllegalArgumentException();
        } else {
            this.day = day;
            this.month = month;
            this.year = year;
        }
    }
    public void addDays(int days) {
        int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int step = 1;
   
        if(days < 0)
            step = -1;
  
        if(isLeapYear(year))
            daysOfMonth[1] = 29;

        int d = 0;
        while(d < days){
            d++;
            day += step;
            if(day > daysOfMonth[month-1]){
                day = 1;
                month++;
                if(month > 12){
                    year++;
                    month = 1;
                    if(isLeapYear(year))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
            }
            else if(day < 1) {
                month--;
                if(month == 0) {
                    month = 12;
                    year--;
                    if(isLeapYear(year))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
                day = daysOfMonth[month-1];
            }
        }
    }
    public void addWeeks(int weeks) {
        addDays(weeks * 7);
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public boolean isLeapYear() {
        return isLeapYear(this.year);
    }
    public boolean isLeapYear(int year) {
        return(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    }
    public int daysTo(Date other) {
        int days = 0;
        int d1, m1, y1, d2, m2, y2;
        int sign = 1;
   
        if(this.toString().compareTo(other.toString()) > 0){
            d1 = other.day;
            m1 = other.month;
            y1 = other.year;

            d2 = day;
            m2 = month;
            y2 = year;
            sign = -1;
        } else {
            d1 = day;
            m1 = month;
            y1 = year;

            d2 = other.day;
            m2 = other.month;
            y2 = other.year;
        }

        int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeapYear(y1))
            daysOfMonth[1] = 29;

        while(d1 != d2 || m1 != m2 || y1 != y2){
            days++;
            d1++;
            if(d1 > daysOfMonth[m1-1]){
                d1 = 1;
                m1++;
                if(m1 > 12){
                    y1++;
                    m1 = 1;
                    if(isLeapYear(y1))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
            }
        }

        days = days * sign;
        return days;
    }

    public String longDate() {
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        return months[month-1] + " " + day + ", " + year;
    }
    public String toString() {
        String s = month + "/" + day + "/" + year;
        return s;
    }
    public static int daysTo(Date one, Date two) {
        return one.daysTo(two);
    }

}

Tester Program测试程序

public class Assign8B {
// Part of the main method I'll use to test your class
// NO imports allowed from the JAVA API
public static void main(String[] a) {
    Date one = new Date(10,15,1582);    // start of Gregorian
    Date two = new Date(1,28,2020); // 2020 is a leap year
    
    one.addDays(1);     // advance one day (negative subtracts days)
    one.addWeeks(10);   // advance one week (negative allowed, yes)
    System.out.println(two.daysTo(one)); // -159645 days (negative)
    System.out.println(one.getDay());   // day is now the 25th (advanced)
    System.out.println(one.getMonth()); // returns 12, January is 1
    System.out.println(one.getYear());  // still 1582 from start
    System.out.println(one.isLeapYear());   // false for 1582
    System.out.println(one.toString()); // style is 12/25/1582
    
    try {
        Date three = new Date(12,33,1956); // obviously illegal
        Date four = new Date(2,29,2013); // illegal leap year
        three.setDay(31);       // fixes that day of month, OK
        four.setMonth(3);       // fixes the month, year still wrong
        four.setYear(1929);     // fixes the year, code not reached
    } catch (IllegalArgumentException e) {
        System.out.println("Illegal day attempted");
    }
    
    // Use UNIX zero of 01/01/70 for default, and create "longDate" output
    // I thought a long date was dinner with a person you don't like?
    Date five = new Date();
    System.out.println(five.longDate());  // January 1, 1970
    
    // Finally, let's understand what static methods are most commonly used for:
    System.out.println(Date.daysTo(one, two));  // still 159645 days (positive)
}

} }

First of all thanks for your interest in SO, you might have seen your problem by now, but if you don't I will explain it, as @madprogrammer mentioned in the comments you have to change your Date call because of the order of year month day in your own class constructor and then look at your add days function and change how you add years in your code.首先感谢您对 SO 的兴趣,您现在可能已经看到了您的问题,但是如果您没有看到,我会解释它,正如@madprogrammer 在评论中提到的那样,由于年份的顺序,您必须更改日期调用在您自己的 class 构造函数中的月份日期,然后查看您的添加日期 function 并更改您在代码中添加年份的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM