如何计算某年某月的第一天是星期几?
下面这段代码我的目的是就算某年某月的第一天是星期几,可是不对,我是按照这种方法做的 :1900年1月1日星期一 作为标杆然后算当月第一天距1900.1.1多少天,取余7,不知道哪里有问题,求指教啊
package rili;
import java.util.Scanner;
public class rili {
boolean isRunNian(int year) {
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
return true;
else
return false;
}
int calcDayOfMonth(int year, int month) {
int days = 0;
int sum = 0;
for (int i = 1; i <= month; i++) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (isRunNian(year))
days = 29;
else
days = 28;
}
sum += days;
}
return sum;
}
int calcDays(int year, int month) {
int sum = 0;
int days;
for (int i = 1900; i < year; i++) {
if (isRunNian(i)) {
sum += 366;
} else {
sum += 365;
}
}
// 对月份中的天数进行处理
days = calcDayOfMonth(year, month);
sum += days;
return sum;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
rili r=new rili();
Scanner s = new Scanner(System.in);
int year;
int month;
year = s.nextInt();
month = s.nextInt();
int sum=r.calcDays(year,month-1);
System.out.println((sum)%7+1);
}
}