欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

输入年月,输出该月份的日历页

程序员文章站 2022-05-07 21:55:04
...

输入年月,输出该月份的日历页

输入输出说明:
输入:
2017 2
输出:
                  1    2    3    4
    5    6    7    8    9   10   11
   12   13   14   15   16   17   18
   19   20   21   22   23   24   25
   26   27   28

import java.util.Calendar;
import java.util.Scanner;
public class Main{
    public static void main(String args[]){
		Calendar cal = Calendar.getInstance();
		Scanner scan = new Scanner(System.in);
		int year,m,days = 0;
		year = scan.nextInt();
		m = scan.nextInt();
		cal.set(year,m-1,1);
		int day = cal.get(Calendar.DAY_OF_WEEK)-1;
		int j = m;
		switch(j){
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
			days=31;
			break;
		case 2:
			if((year%100!=0) && (year%4==0) || (year%400==0))
				days = 29;
			else
				days=28;
			break;
			default:
				days=30;
				break;
		}
		int ii = 1;
		for(;ii<=day;ii++)
			System.out.print("     ");
		for(int k=1;ii<=day+days;ii++){
			System.out.printf("%5d",k++);
			if(ii%7==0)
				System.out.println();
		}
	}
	
}


相关标签: 日历