public class hw4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("年份");
int year = sc.nextInt();
System.out.println("月份");
int month = sc.nextInt();
// 判断闰年
int runnian = judgeLeap(year);
//System.out.println("该年份的2月天数" + runnian);
// 根据月份判断该月的天数
int days = getDay(month, runnian);
// 判断输入年份距1900年1月1日的天数
int daynum = getDayNum(year);
//System.out.println("距离1900年的天数" + daynum);
// 判断所输入月份距1月1日的天数
int daynum1 = getDayNum1(month, runnian);
//System.out.println("距离 1月1日天数" + daynum1);
// 得到距离1900年1月1日的总天数
int daysum = getSum(daynum, daynum1, days, runnian);
//System.out.println("距离1900年1月1日的总天数" + daysum);
// 计算该月第一天是星期几
int week = getWeek(daysum);
//System.out.println("该月第一天是星期" + week);
printWeek();
calendar(days, week);
}
/**
* //判断是否为闰年
*
* @param year
* @return
*/
public static int judgeLeap(int year) {
int a;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
a = 29;
} else {
a = 28;
}
return a;
}
//获取该月的天数
public static int getDay(int month, int feb) {
int days;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
days = feb;
break;
default:
days = 30;
break;
}
return days;
}
//用循环计算用户输入的年份距1900年1月1日的天数
public static int getDayNum(int year) {
int j = 0;
int d;
for (int i = 1900; i <= year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
j++;
} else {
continue;
}
}
d = j * 366 + ((year - 1900 + 1 - j) * 365);
return d;
}
//判断所输入月份与1月1日距离多少天
public static int getDayNum1(int month, int runnian) {
int days = 0;
for (int i = 1; i <= month; i++) {
int day = getDay(i, runnian);
days += day;
}
return days;
}
/**
* 计算总天数
*
* @param daynum
* @param daynum1
* @param days
* @param runnian
* @return
*/
public static int getSum(int daynum, int daynum1, int days, int runnian) {
int d = daynum - (337 + runnian) + daynum1 - days;
return d;
}
/**
* 计算星期几
*
* @param daysum
* @return
*/
public static int getWeek(int daysum) {
int week = (daysum + 1) % 7;
return week;
}
public static void printWeek() {
String[] arr = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println("");
}
/**
* 初始化日历
*
* @param days:这个月的天数
* @param week:该月第一天是星期几
*/
public static void calendar(int days, int week) {
int count = 0;
for (int i = 0; i < week; i++) {
count++;
System.out.print("\t");
}
for (int i = 0; i < days; i++) {
count++;
System.out.print((i + 1) + "\t");
if (count % 7 == 0) {
System.out.println(" ");
}
}
}