package demo;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.println("请输入年:");
int year =sc.nextInt();
System.out.println("请输入月:");
int month = sc.nextInt();
System.out.println("请输入日:");
int days = sc.nextInt();
//获得这个年月的1号是星期几
int week = getWeek(year,month,days);
System.out.println("日 一 二 三 四 五 六");
System.out.println();
int count = 0;
//打印空格
for(int i = 0; i < week;i++){
count++;
System.out.print("\t");
}
//获得这个年月的天数
int days1 = getDays(year, month);
for(int i =1; i <= days1;i++){
count++;
System.out.print(i+"\t");
if(count == 7){
System.out.println();
System.out.println();
count = 0;
}
}
System.out.println();
}
//星期
/**
week = (year-1+(year-1)/4-(year-1)/100+(year-1)/400+days)%7
*/
public static int getWeek(int year, int month, int days){
//先计算指定年月日的是这一年的第多少天
int totalDays = getTotalDays(year, month, days);
int week = (year-1+(year-1)/4-(year-1)/100+(year-1)/400+totalDays)%7;
return week;
}
//给定年月日 求出是第几天
public static int getTotalDays(int year,int month,int days){
//定义一个天数的和 31+29+30
int totalDays = 0;
//从1月循环到当前月-1,天数求和 +当前天数
for(int i =1;i < month;i++){
int mdays = getDays(year,i);
totalDays = mdays + totalDays;
}
totalDays += days;
return totalDays;
}
//给定年月,求出天数
public static int getDays(int year,int month){
int mdays = 0;
switch(month){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
mdays = 31;
break;
case 4 :
case 6 :
case 9 :
case 11 :
mdays = 30;
break;
case 2:
//可以被400整除或者能被4整除但是不能被100整除
if(year%400 == 0 || (year%4 == 0 && year%100 != 0)){
mdays = 29;
}else{
mdays = 28;
}
break;
}
return mdays;
}
}