JAVA 输入年份月份打印该月的日历

时间:2022-07-19 11:30:27

请输入年份:yyyy

请输入月份:MM

打印出yyyy-MM日历

<span style="font-size:18px;">package qq;

import java.util.Calendar;
import java.util.Scanner;

public class time {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar calendar = Calendar.getInstance();
		int count = 1;
		int days;
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入年份");
		int year = sc.nextInt();
		System.out.print("请输入月份");
		int m = sc.nextInt();
		int month=m-1;
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DATE, 0);
		if (((year%4==0&&year%100!=0)||(year%400==0))&&m==2) {
		 days=29;
		}else{
		 days = chooseMonth(month + 1);}
		System.out.println(year + "年" + (month + 1) + "月的日历:");
		System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
		while (count <=days) {
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			int day = calendar.getTime().getDay();   //某个月从第几天开始

			if (count ==1) {
				for (int i = 0; i < day; i++) {
					System.out.print("\t");
				}
			}
			if (day == 0) {
				System.out.println();        //出现星期天  就换行,外国人周天是一个周开始
			}
			System.out.print(calendar.getTime().getDate() + "\t");  //每月多少号
			count++;

		}

	}

	public static int chooseMonth(int m) {
		int days = 0;
		switch (m) {
		case 2:
			days = 28;
			break;

		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;

		case 4:
		case 6:
		case 9:
		case 11:
			days = 30;
			break;
		default:
			days = 0;
		}
		return days;
	}
}</span>

JAVA 输入年份月份打印该月的日历

JAVA 输入年份月份打印该月的日历

JAVA 输入年份月份打印该月的日历

JAVA 输入年份月份打印该月的日历


作者:冲天之峰                 20160527