//万年历功能类
public class MyCalender {
//判断润平年功能
public boolean isRun(int year){
//普通年,被4整除,且%100!=0,是闰年
if (((year%4)==0) && ((year%100)!=0)){
//System.out.println(year+"是闰年");
return true;
}
if (year%400==0){
//System.out.println(year+"是闰年");
return true;
}
//System.out.println(year+"是平年");
return false;
}
//输入一个年,月,返回这个月有多少天
public int getDays(int year, int month){
int days=30;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days=31;
}else if (month==4||month==6||month==9||month==11){
days=30;
}else {
boolean r=isRun(year);
if (r){
days=29;
}else {
days=28;
}
}
return days;
}
//1900,1,1日是周1,往后算,所要计算的年月日到1900,1,1日多少天,与%7,即可的到
//输入年月,计算这个year,month,1号到1900,1,1总共有多少天
private int getTotal(int year,int month){
int total=0;
//循环从1900到year-1年,的12月31日,在这个循环中判断每次的值是闰年还是平年,闰年则+366,否则365
//计算从year 年到1900年中有多少天
for (int i=1900;i<year;i++){
if (isRun(i)){
total+=366;
}else {
total+=365;
}
}
//计算yearn年moth月,到year年1月有多少天
//从1到12循环,累加getDays(年,月)
for (int j=1;j<month;j++){
total+=getDays(year, j);
}
//在加1
return total+1;
}
//求某个总天数对应是周几
private int getWeekday(int total){
return total%7;
}
public void showCalender(int year,int month){
int days=getDays(year,month);//year年month月有多少天
System.out.println(year+"年"+month+"月有:"+days+"天");
int total=getTotal(year,month);
System.out.println("到1900年1月1日总共有:"+total+"天");
int weekday=getWeekday(total);
System.out.println("周"+weekday);
//显示日历
System.out.println(year+"年"+month+"月");
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i=0;i<weekday;i++){
System.out.print("\t");
}
for (int i=1;i<=days;i++){
System.out.print(i+"\t");
if ((i+weekday)%7==0){//(月份号数+周几数)%7==0,换行
System.out.println();
}
}
}
}
//main