为什么Jan Month的任何一天在我的代码中给我错误的一天?(仅发生在1994年)
public class Ques1
{
public static void main(String args[])
{
//假设我们知道2007年6月6日那天是星期三,将0,1,2,3 ......分配给星期一,星期二,星期三......太阳
int my_birth_year = 1996;
int my_birth_month = 1;
int my_birth_date = 2;
int j = 2;
for(int i = 2006;i>=(my_birth_year);i--)
{
if((i+1)%4==0 || (i+1)%400==0)
{j = ((j-2)+7)%7;if(j<0) j = j*-1;}
else if(i%100==0)
{j = ((j-1)+7)%7;if(j<0) j = j*-1;}
else
{j = ((j-1)+7)%7;if(j<0) j = j*-1;}
}
System.out.println(j);//day on 6th June my_birth_YEAR
int june = 6;
int days_june = 30;
if( june< my_birth_month)
{
int d,s;
int h = my_birth_month - june;
switch(h)
{
case 1 : d = 24 + my_birth_date;
j = (j+(d%7))%7; break;
case 2 : d = 24 + 31 + my_birth_date;
j = (j+(d%7))%7; break;
case 3 : d = 24 + 31+ 31+my_birth_date;
j = (j+(d%7))%7; break;
case 4 : d = 24 + 31 +31+30+ my_birth_date;
j = (j+(d%7))%7; break;
case 5 : d = 24 + 31+31+30+30+my_birth_date;
j = (j+(d%7))%7; break;
case 6 : d = 24 + 31 +31+30+31+30+ my_birth_date;
j = (j+(d%7))%7; break;
default : break;
}
}
else if(june > my_birth_month)
{
int d;
int h = june - my_birth_month;
switch(h)
{
case 1 : d = 6+(31-my_birth_date);
j = (-j+(d%7))%7; break;
case 2 : d = 6 + 31 +30 - my_birth_date;
j = (-j+(d%7))%7; break;
case 3 : d = 6 + 31+ 30+31-my_birth_date;
j = (-j+(d%7))%7; break;
case 4 :if(my_birth_year %4 == 0 || my_birth_year%400 == 0)
d = 6 + 31 +30+31+ 29 - my_birth_date;
else
d = 6 + 31 +30+31+ 28 - my_birth_date;
j = (-j+(d%7))%7; break;
1月就是这种情况:
case 5 :if(my_birth_year %4 == 0 || my_birth_year%400 == 0)
d = 6 + 31 +30+31+ 29 +31 - my_birth_date;
else
d = 6 + 31 +30+31+ 28 + 31 - my_birth_date;
j = (-j+(d%7))%7; break;
default : break;
}
}
if(j<0) j = j*-1;
switch (j)
{
case 0 : System.out.println("Monday");
break;
case 1 : System.out.println("Tuesday");
break;
case 2 : System.out.println("Wednesday");
break;
case 3 : System.out.println("Thursday");
break;
case 4 : System.out.println("Friday");
break;
case 5 : System.out.println("Saturday");
break;
case 6 : System.out.println("Sunday");
break;
default : System.out.println("Error");
break;
}
}
}
P.S。 :我正在编写代码而不使用API