package judgeWeekday; import java.util.Scanner; public class JudgeWeekday { //参考点选择2014年1月1日星期三具有一定的特殊性;代码也与之有关。 public static final int YEAR0 = 2014; //public static final int MONTH0 = 1; //public static final int DAY0 = 1; public static final int WEEKDAY0 = 3; public static final String week[]={"天","一","二","三","四","五","六"}; public static final int leapmonthday[]={31,29,31,30,31,30,31,31,30,31,30,31}; public static final int noleapmonthday[]={31,28,31,30,31,30,31,31,30,31,30,31}; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("input the date like 1983 3 29: "); int year = console.nextInt(); int month = console.nextInt(); int day = console.nextInt(); boolean ynleap = judgeleap(year); int weekday = judgeWeekday (year,month,day,ynleap); System.out.println( year + "年" + month + "月" + day +"日是星期" + week[weekday]); } public static int judgeWeekday (int year,int month,int day,boolean ynleap) { int sum = computeDaysDistance(year,month,day,ynleap); int weekday = getWeekday(sum,year); return weekday; } //计算输入输入日期与参考日期相隔的天数。 public static int computeDaysDistance(int year,int month,int day,boolean ynleap) { int sum = 0,sum1 = 0,sum2 = 0; if(year < YEAR0) { //计算sum1 for(int i = year + 1;i < YEAR0;i++) { if(judgeleap(i)) sum1 += 366; else sum1 += 365; } //计算sum2 if(ynleap) { sum2 = leapmonthday[month - 1]- day +1; for(int j = month + 1;j <=12;j++) sum2 += leapmonthday[j - 1]; } else { sum2 = noleapmonthday[month -1]- day +1; for(int j = month + 1;j <=12;j++) sum2 += noleapmonthday[j - 1]; } } else { //计算sum1 for(int i = YEAR0;i < year;i++) { if(judgeleap(i)) sum1 += 366; else sum1 += 365; } //计算sum2 sum2 = day -1; for(int j = 1;j <= month -1;j++) { if(ynleap) sum2 += leapmonthday[j - 1]; else sum2 += noleapmonthday[j -1]; } } //计算sum sum =sum1 + sum2; return sum; } //给与参考日期相隔的天数得到输入日期是星期几。 public static int getWeekday(int sum,int year) { int weekday = 0; if(year < YEAR0) weekday = (WEEKDAY0 + 7 - (sum %7))%7; else weekday = (WEEKDAY0 + (sum % 7))%7; return weekday; } //判断给定的year是否闰年。 public static boolean judgeleap(int year) { boolean ynleap; if(((year % 4 ==0)&&(year % 100!= 0))||(year % 400 == 0)) ynleap = true; else ynleap = false; return ynleap; } }//class