【问题标题】:Dates check in loop?日期签入循环?
【发布时间】:2018-09-02 21:25:10
【问题描述】:

我有一个任务是使用给定的方法请求创建一个 Date 类。 其中一种方法以整数形式返回星期几,其中 1=Sunday.....6=Friday,0=Saturday。 方法是这样的:

public int dayInWeek () {
    int day, month;
    int year;    //2 last numbers of the year
    int century; //2 first numbers of the year
    if(_month==1 || _month==2) {
        day= _day;
        month= _month + 12;
        year= (_year-1) % 100;
        century= (_year-1) / 100;
    }
    else {
        day= _day;
        month= _month;
        year= _year % 100;
        century= _year / 100;
    }

    return (day + (26*(month+1))/10 + year + year/4 + century/4 - 2*century)%7;
} 

现在,在收到星期几后,作业提示可能有某个日期的结果可能是否定的。 我试图制作一个main(),它将在给定的年份 1800-2100 的所有日子和年份中循环,但只会变得越来越困惑和迷失。 如果有人能提示我如何实现它,我将不胜感激,而无需所有现有的日历/日期/等类,因为它对我来说就像这样变得一团糟。 谢谢。

编辑:更多信息:

//constructors
/**
 * creates a new Date object if the date is valid, otherwise creates the date 1/1/2000
 * @param _day the day of the month (1-31)
 * @param _month the month in the year (1-12)
 * @param _year the year (1800-2100)
 */
public Date (int day, int month, int year) {
    if (isValidDate (day, month, year)) {
        _day=day;
        _month=month;
        _year=year;           
    }
    else {
        setToDefault();
    }
}

/**
 * copy constructor 
 * @param other the date to be copied
 */
public Date (Date other) {
    _day= other._day;
    _month= other._month;
    _year= other._year;
}
//methods
private boolean isValidDate(int day, int month, int year) {
    if (year<MIN_YEAR || year>MAX_YEAR) {
        return false;
    }
    if (month<MIN_MONTH || month>MAX_MONTH) {
        return false;
    }
    if (day<MIN_DAY) {
        return false;
    }
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: return day<=31;
        case 4:
        case 6:
        case 9:
        case 11: return day<=30;

        default: return leap(year) ? day<=29 : day<=28; //month==2;
    }        
}

/**
 * check if leap year
 * @param y year to check
 * @return true if given year is leap year
 */
private static boolean leap (int y) {
    return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}

希望这就够了。

【问题讨论】:

  • 能否请您添加缺少的部分,即_month,'_year' 上的信息。这些值是如何定义的?
  • @StefanFreitag 嘿,刚刚添加了构造函数及其使用的方法。 _day/_month/_year 是 Date 类的实例变量
  • 从您的一般描述中很难理解您为什么会感到困惑。您能否添加一些具有预期结果的日期示例以及观察到的结果有何不同?我相信它会有所帮助。
  • @OleV.V.那就是问题所在。提示清楚地表明 dayInWeek 可能会返回负数,我所有的手动检查都产生了正确的结果。这就是为什么我试图让程序运行所有日期,如果 dayInWeek 的结果是否定的,请打印该日期,以便我可以查看导致问题的原因。但这也是对我来说真正混乱的地方,我迷失了方向,对如何正确进行贯穿检查感到困惑。例如,d1.dayInWeek(),其中 d1 是一个 Date 对象,其中包含日期 24.3.18,将产生整数 6(=friday)。

标签: java loops validation date


【解决方案1】:

当年份为 2100 时,您的结果为负数。使用今天的日期(月 = 3,日期 = 24,年 = 2100)进行测试,您可以看到它给出了一个负数。

如果您查看Code implementation for Zeller's congruence,您可以通过将公式更改为:

return dayOfWeek= (day + (26*(month+1))/10 + year + year/4 + century/4+ 5*century)%7;

【讨论】:

  • 想解释一下为什么 -2 到 +5 的变化会影响天数计算,以及您是如何得出该公式的?
  • 当然,更新了答案,所以有来源,请检查一下。希望对您有所帮助!
猜你喜欢
  • 2019-05-12
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 2015-10-13
  • 2011-07-04
  • 2020-12-26
相关资源
最近更新 更多