由某日期计算星期几 java实现

本程序是对照1980/1/1星期二   推算的

 

public class ChineseWeek {
    public static void main(String[]args)
    {
    	int year=2010,month=4,day=2;
    	boolean leap=year%400==0||year%4==0&&year%100!=0;
    	int total=year-1980+(year-1980+3)/4;
    	//若Month==12,直接求前11个月总天数。
    	//若month==1,直接加上day     所以switch循环条件为month-1
    	switch(month-1)
    	{
    	case 11:total+=30;
    	case 10:total+=31;
    	case 9 :total+=30;
    	case 8:total+=31;
    	case 7:total+=31;
    	case 6:total+=30;
    	case 5:total+=31;
    	case 4:total+=30;
    	case 3:total+=31;
    	case 2:total+=leap ? 29:28;
    	case 1:total+=31;
    	case 0:total+=day;
    	}
    	int week=1;
    	//week=(week+total)%7;
    	week=total%7;
    	
    	System.out.print(year+"年"+month+"月"+day+"日   星期");
    	
    	
    	switch(week)
    	{
    	case 0:System.out.println("一");break;
    	case 1:System.out.println("二");break;
    	case 2:System.out.println("三");break;
    	case 3:System.out.println("四");break;
    	case 4:System.out.println("五");break;
    	case 5:System.out.println("六");break;
    	case 6:System.out.println("日");break;
    	
    	}
    	/*switch(week)
    	{
    	case 0:System.out.println("日");break;
    	case 1:System.out.println("一");break;
    	case 2:System.out.println("二");break;
    	case 3:System.out.println("三");break;
    	case 4:System.out.println("四");break;
    	case 5:System.out.println("五");break;
    	case 6:System.out.println("六");break;
    	
    	}*/
    	
    }

}


 

你可能感兴趣的:(由某日期计算星期几 java实现)