public class Calendar_04 {
public static int getDaysOfMonth(int year, int month){
switch(month){
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeap(year) ? 29 : 28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return -1;
}
}
/**
* 计算自1990 年到所求月份1号之前的的天数
* @return
*/
public static int getTotalDay(int year, int month){
int sum = 0;
for (int i = 1990; i < year; i++) {
for (int j = 1; j <= 12; j++) {
sum += getDaysOfMonth(i, j);
}
}
for(int i = 1; i < month; i++){
sum += getDaysOfMonth(year, i);
}
return sum;
}
public static boolean isLeap(int year){
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0){
return true;
}else {
return false;
}
}
// 日历输前的空格
public static int numOfBlank(int year, int month){
return ((getTotalDay(year, month) % 7) + 1) % 7;
}
public static void print(int year, int month){
int n = numOfBlank(year, month);
int count = n;
for (int i = 0; i < n; i++) {
System.out.print('\t');
}
for(int i = 1; i <= getDaysOfMonth(year, month); i++){
System.out.print(i + "\t");
count++;
if(count % 7 == 0){
System.out.println();
}
}
}
public static void start(){
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
getTotalDay(year, month);
System.out.println("===================="+year+"年"+month+"月"+"=======================");
System.out.println("Sun\tMon\tThu\tWed\tTues\tFri\tSat");
System.out.println("---------------------------------------------------");
print(year, month);
}
public static void main(String[] args) {
start();
}
}
日历表制作
猜你喜欢
转载自blog.csdn.net/qq_41936045/article/details/81023862
今日推荐
周排行