java 程序写万年历

万年历

package java01;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.spi.CalendarDataProvider;

public class Year {
    public static void main(String[] args) {
		System.out.println("请输入年份:");
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		System.out.println("请输入月份:");
		int month = input.nextInt();
	
	//创建calendar
	Calendar calendar =Calendar.getInstance();
	calendar.set(year, month-1, 1);
	
	int week = calendar.get(Calendar.DAY_OF_WEEK);
	
	int days=30;
	switch (month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		days=31;
		break;
		//判断是否为闰年
	case 2:
		GregorianCalendar gc = new GregorianCalendar();
		boolean flag = gc.isLeapYear(year);
		if (flag) {
			days=29;
		}else {
			days=28;
		}
		break;
		
	default:
		days=30;
		break;
	}
	
	//打印表头
	System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
	//打印week-1个制表符
	for (int i = 0; i < week-1; i++) {
		System.out.print("\t");
	}
	
	//打印天数
	for (int i = 1; i 

你可能感兴趣的:(java 程序写万年历)