package com.haliluya.java.oj;
import java.util.Scanner;
public class judgyDay {
private static final int startYear = 1900;
private static final int[][] dayMap = { {0,1,2,3,4,5,6},
{1,2,3,4,5,6,0},
{2,3,4,5,6,0,1},
{3,4,5,6,0,1,2},
{4,5,6,0,1,2,3},
{5,6,0,1,2,3,4},
{6,0,1,2,3,4,5}
};
public static void main
(String[] args
) {
Scanner s
= new Scanner
(System.
in);
int year = s.nextInt();
int month = s.nextInt();
int hao = s.nextInt();
System.
out.
println("输入的年 = "+year
);
System.
out.
println("输入的月 = "+month
);
System.
out.
println("输入的日 = "+hao
);
int dayOfWant = 0;
dayOfWant = judgyDayOfAnyYearAnyMonth(year,month,hao);
System.
out.
println("对应的星期为 (从0算) = "+dayOfWant
);
}
/**
* 根据输入的年 月以及号 得到改天的星期数
* @param year
* @param month
* @param hao
* @return
*/
private static int judgyDayOfAnyYearAnyMonth(int year, int month, int hao) {
//参数检查
if(year< 1900 ||year >9999 ){
return -1;
}
if(month<0 ||month >12){
return -1;
}
if(hao<0 || hao>31 ){
return -1;
}
//业务开始
//1 算出该月1号距离1900年1月1号多少天
int daysOfLastMonthTotal = gatDaysOfLastMonthTotal(year,month);
//2 的出该月1号是星期几
int firstDay = daysOfLastMonthTotal %7;
System.
out.
println("给定的月初星期几(从0算) = "+(firstDay
));
//3 算出该号是星期几
int dayJudgyByFirstDay = getDayJudgyByFirstDay(firstDay,hao);
return dayJudgyByFirstDay;
}
private static int getDayJudgyByFirstDay(int firstDay, int hao) {
hao = hao%7;
if(hao ==0){
hao =7;
}
hao -=1;//调整到0下标
System.
out.
println("取map的第 "+firstDay
+ "行,第"+ hao
+"列");
return dayMap[firstDay][hao];
}
/**
* 算出给定年月的第一天的距离1900的天数
* @param year
* @param month
* @return
*/
private static int gatDaysOfLastMonthTotal(int year, int month) {
// 先算年的求和
int totalday = 0;
for(int thisYear = startYear+1 ;thisYear<=year ;thisYear++){
totalday += getTotalDaysByYear(thisYear);
}
//再不上月的求和
for(int i = 1;i <month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: totalday+= 31;break;
case 2:
if(isRunYear(year)){
totalday+= 29;break;
}else
{
totalday+= 28;break;
}
case 4:
case 6:
case 9:
case 11:totalday+= 30;break;
}
}
System.
out.
println("给定年月的第一天距离1900的天数 = "+totalday
);
return totalday;
}
/**
* 判断该年有多少天
* @param thisYear
* @return
*/
private static int getTotalDaysByYear(int thisYear) {
if((thisYear%4 ==0 && thisYear %100!= 0)||thisYear%400 ==0){
return 366;//二月多一天 29天
}
return 365;
}
private static boolean isRunYear(int thisYear){
return ((thisYear%4 ==0 && thisYear %100!= 0)||thisYear%400 ==0);
}
}
//java/8493