Java中的工作日查找器

 fdsafjlkjgklg_431 发布于 2023-02-13 12:36

我正在做一个项目,包括:'编写一个提示日期(月,日,年)的程序,并报告该日期的星期几.知道1601年1月1日是星期一可能会有所帮助.这是一本"构建Java程序 - 回归基础知识方法,第2版"的练习,这本书是我自学Java自学的书.任何反馈都受到高度赞赏,但我确实要求你解释为什么你会以某种方式做某事.谢谢!

所以,我的问题是,对于接近1600年的日期,它给出了正确的一天(我相信),最近几天也是如此,他们有三天的偏移(至少我检查过的那些).为什么会发生这种情况,我该如何解决?谢谢!

我的代码:

// finds the day of the week of the given date
public static String dayFinder(int month, int day, int year) {
    // handle invalid input
    if (month > 12 || month < 1 || day > 31 || day < 1) {
        throw new IllegalArgumentException("Month must be between "
                + "1 and 12 and Day must be between 1 and 31.");
    }

    // convert to "absolute" day, covering day and month
    int absoluteDay = monthToDay(month, day, year);

    // convert year to days and add to "absolute" day
    absoluteDay += yearToDay(year);

    if (absoluteDay % 7 == 1) {
        return "Monday";
    } else if (absoluteDay % 7 == 2) {
        return "Tuesday";
    } else if (absoluteDay % 7 == 3) {
        return "Wednesday";
    } else if (absoluteDay % 7 == 4) {
        return "Thursday";
    } else if (absoluteDay % 7 == 5) {
        return "Friday";
    } else if (absoluteDay % 7 == 6) {
        return "Saturday";
    } else { // absoluteDay % 7 == 0
        return "Sunday";
    }
}

// calculates the number of days present in a given
// date since the beginning of the year
public static int monthToDay(int month, int day, int year) {

    // convert to "absolute" day
    int absoluteDay = 0, daysTo31 = 0;

    // iterate through months
    for (int i = 0, loopMonth = month; i < month; i++) {
        if (loopMonth == 1 || loopMonth == 3 || loopMonth == 5 
                || loopMonth == 7 || loopMonth == 8 || loopMonth == 10
                || loopMonth == 12) {
            absoluteDay += 31;
            daysTo31 = 0;
        } else if (loopMonth == 2) {
            if (year % 4 != 0) {
                absoluteDay += 28;
                daysTo31 = 3;
            } else { // leap year
                absoluteDay += 29;
                daysTo31 = 2;
            }
        } else { // month = 4, 6, 9 or 10
            absoluteDay += 30;
            daysTo31 = 1;
        }
        loopMonth--;
    }

    // adjust to specific day
    absoluteDay -= (31 - day - daysTo31);       
    return absoluteDay;
}

// calculates the number of days between 
// (the beginning of) the given year and
// (the beginning of) the reference year 1601
public static int yearToDay(int year) {

    // convert to "absolute" day
    int absoluteDay = 0;
    year -= 1601; 

    // iterate through years
    for (int i = 0, loopYear = year; i < year; i++) {
        if (loopYear % 4 != 0) {
            absoluteDay += 365;
        } else { // leap year
            absoluteDay += 366;
        }
        loopYear--;
    }

    return absoluteDay;
} 

// 1604年(MDCIV)是周四开始的闰年

1 个回答
  • 你的问题可能与闰年有关.

    由于维基百科:

    Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year. [链接]

    这就是你有三天太多的原因(1700,1800,1900).

    2023-02-13 12:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有