转载:[url]http://hi.baidu.com/triceratops/blog[/url]
public class WeekDay{
public static void showWeekDay(int year, int month, int day){
char[] ch = new char[] {'日','一','二','三','四','五','六'};
int eclipseDays = 1; //公元元年一月一日是星期一
eclipseDays += year - 1;
for(int i=1;i<year;i++){
if(isBissextile(i))
eclipseDays++;
}
switch(month){
case 1: break;
case 2: eclipseDays+=3; break;
case 3: eclipseDays+=3; break;
case 4: eclipseDays+=6; break;
case 5: eclipseDays+=8; break;
case 6: eclipseDays+=11; break;
case 7: eclipseDays+=13; break;
case 8: eclipseDays+=16; break;
case 9: eclipseDays+=19; break;
case 10: eclipseDays+=21; break;
case 11: eclipseDays+=24; break;
case 12: eclipseDays+=26;
}
if(isBissextile(year) && month>2)
eclipseDays++;
eclipseDays = (eclipseDays + day -1)%7;
System.out.println(year+"年"+month+"月"+day+"日是星期"+ch[eclipseDays]);
}
public static boolean isBissextile(int year){
boolean bissextileFlag = false;
if(year%4 == 0){
if(year%100 != 0)
bissextileFlag = true;
else if(year%400 == 0)
bissextileFlag = true;
}
return bissextileFlag;
}
public static void main(String[] args) throws Exception{
if(args.length != 3){
System.out.println("Usage: java WeekDay yyyy MM dd");
System.exit(0);
}
showWeekDay(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]));
}
}