Java:计算星期几适用于本月以外的所有日期

时间:2013-07-29 20:59:22

标签: java calendar

我正在为我的编程课程编写一个程序,其目标是计算输入的一天和一个月的星期几。我必须在不使用Calendar类的情况下执行此操作。

我的代码适用于我提供的任何月份/年份,本月(2013年7月)除外。我无法弄清楚为什么会这样。任何人都可以帮忙吗? 输出的例子如下:

错误的例子

run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2013
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 2
First day of the year: 1
BUILD SUCCESSFUL (total time: 5 seconds)

正确的例子

run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2012
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 1
First day of the year: 0
BUILD SUCCESSFUL (total time: 5 seconds)

我已链接了可下载的Java文件here

package calendar;

/**
 *
 * @author Michael Dornisch
 */
import java.util.Scanner;

public class Calendar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Initiating vars
        int inMonth;
        int inYear;

        //Initiating Scanner
        Scanner input = new Scanner(System.in);

        System.out.println("Gregorian Calendar Creator"); //Just a print line

        /* This block gets a month. Instead of exiting when the user inputs an 
         * invalid month, it instead asks again untl a proper month is used.
         */
        System.out.print("Enter a month: ");
        inMonth = input.nextInt();
        while (inMonth > 12 || inMonth <= 0) {
            System.out.println("ERROR: Improper month. Please enter a proper numerical month (1-12).");
            System.out.print("Enter a month: ");
            inMonth = input.nextInt();
        }
        /* This block gets a year. Instead of exiting when the user inputs an 
         * invalid year, it instead asks again untl a proper year is used.
         */
        System.out.print("Enter a year: ");
        inYear = input.nextInt();
        while (inYear < 1752) {
            System.out.println("ERROR: Improper Year. Please enter a year that is 1753 or greater.");
            System.out.print("Enter a year: ");
            inYear = input.nextInt();
        }
        /* Here is where I am testing everything
         * 
         */
        Calendar newCal = new Calendar();
        System.out.println("0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat");
        System.out.println("Test this in a UNIX terminal by typing 'cal July 2013' or similar date");
        System.out.println("First day of the month: " + newCal.firstDayOfMonth(inMonth, inYear));
        System.out.println("First day of the year: " + newCal.firstDayOfYear(inYear));
    }

    public boolean leap(int inputYear) {

        /* This IF is checked first. If the year is divisible by 100, it isn't a 
         * leap year unless it is also divisible by 400. 
         */
        if (inputYear % 100 == 0) {
            if (inputYear % 400 == 0) {
                return (true);
            } else {
                return (false);
            }
        }
        //Any other number that's divisible by 4 is also a leap year
        if (inputYear % 4 == 0) {
            return (true);
        } else {
            return (false);
        }
    }

    public int firstDayOfYear(int inputYear) {
        /*
         * Here is a formula for finding the day of the week for ANY date.
         * N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
         * N = day of week
         * d = day of month
         * m = number of month
         * y = year
         *  mod 7 for answer
         * 0 = sunday, 1 = monday, etc.
         */
        int day = 5 + (6 / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
        day = day % 7;
        return (day);
    }

    public int firstDayOfMonth(int inputMonth, int inputYear) {
        /*
         * Here is a formula for finding the day of the week for ANY date.
         * N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
         * N = day of week
         * d = day of month
         * m = number of month
         * y = year
         *  mod 7 for answer
         * 0 = sunday, 1 = monday, etc.
         */
        int day = 3 + (2 * inputMonth) + ((3 * (inputMonth + 1)) / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
        day = day % 7;
        return (day);
    }
}

2 个答案:

答案 0 :(得分:2)

不确定您的公式是否正确且每天的数字与之匹配。 http://mathforum.org/library/drmath/view/55837.html

N = d + 2m + [3(m + 1)/ 5] + y + [y / 4] - [y / 100] + [y / 400] + 2

我也会使这种方法更通用

 public int firstDayOfYear(int day, int month, int year) {
        int day = day + (2*month) + ((3*(month+1))/5) + year + (year/4) - (year/100) + (year/ 400) +2;
        return day % 7;
    }

 System.out.println("First day of the year: " + newCal.firstDayOfYear(1, 1, inYear));

答案 1 :(得分:1)

永远不会调用leap方法。那可能就是那样。因为这个月的第一天是星期一而不是星期二(就在前一天=&gt;因为去年是闰年)