0

The pseudocode for an algorithm to compute the day of the week for a given date from the year 1753 onwards is as follows. Let d be the day of the month (from 1 up to 31), m be an integer denoting the month of the year (where 1 denotes January, 2 denotes February, and so on), and y denote the year. The algorithm then performs the following steps in order:

If m is less than 3
Add 12 to m and subtract one from y
End if
Set C to be the year of the century (e.g., 10 for the year 2010)
Set D to be the century (e.g., 20 for the year 2010)
Divide 13 * (m + 1) by 5 and call the quotient W
Divide C by 4 and call the quotient X
Divide D by 4 and call the quotient Y
Set Z to be W + X + Y + d + C - 2 * D
Divide Z by 7 and call the remainder day
If day is less than 0
Add 7 to day
End if

The value of day then gives the day of the week, with 0= Saturday, 1= Sunday, up to 6= Friday

the code i have so far is:

public static String dayOfWeek( SimpleDate date ) {
        // TO BE COMPLETED
        int[] d = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
        int[] m = {1,2,3,4,5,6,7,8,9,10,11,12};
        int y = SimpleDate(int year);

        if (m < 3) {
            m + 12;
            y - 1;
        }
        C = SimpleDate(int year[2:3];
        D = SimpleDate(int year[0:1];
        W = 13 * (m + 1) / 5;
        X = C / 4;
        Y = D / 4;
        Z = W + X + Y + d + C - 2 * D;
        day = Z % 7;
        if (day < 0) {
            day + 7;
        }

im not sre how to set the year for C and D and at the beginning aswell. also where i ahve used code such as m + 12 i get an error saying that + is not a statment

4

2 回答 2

2

How about using java.util.Calendar and do

calendar.get(Calendar.DAY_OF_WEEK);
于 2013-02-19T22:48:54.707 回答
1

使用乔达

MutableDateTime dateTimeInstance = new MutableDateTime().setYear(year).setMonth(month).setDay(dayOfMonth); // and so on per [the docs](http://joda-time.sourceforge.net/api-release/org/joda/time/MutableDateTime.html)
String dayName = dateTimeInstance.dayOfWeek().getAsText();
于 2013-02-19T22:51:39.483 回答