Java打印日历_System

package edu.java.homework.calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MyCalendar
{
    private final int year;
    private final int month;
    public MyCalendar(int year,int month)//构造函数
    {
        this.year=year;
        this.month=month;
    }
    public void printCalendar()
    {
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        int week=getWeekDay(year,month,1);//得到星期数
        for(int i=1;i<week;i++)
        {
            System.out.print("\t");
        }
        int monthDays=getMonthDays(year,month);//得到该月天数
        for(int i=1;i<=monthDays;i++)
        {
            if(getWeekDay(year,month,i)==7)
            {
                System.out.print(i+"\n");
            }else
                System.out.print(i+"\t");
        }
        System.out.println();
    }
    public int getWeekDay(int year,int month,int day)
    {
        int week=-1;
        // 使用Calendar类
        Calendar calendar=new GregorianCalendar(year,month-1,day);
        week=calendar.get(Calendar.DAY_OF_WEEK);
        return week;
    }
    public int getMonthDays(int year,int month)
    {
        int monthDays=0;
        if(month==4||month==6||month==9||month==11)
        {
            monthDays=30;
        }else if(month==2) // 2月需要判断是否为闰年
        {
            if(year%4==0&&year%100!=0||year%400==0)
            {
                monthDays=29;
            }else
                monthDays=28;
        }else
            monthDays=31;
        return monthDays;
    }
}
package edu.java.homework.chapter;

import edu.java.homework.calendar.MyCalendar;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Chapter1
{
    Scanner in=new Scanner(System.in);
    public void chapterMenu()
    {
        while(true)
        {
            System.out.println("请选择作业序号:");
            System.out.println("1--输出日历;2--人事管理系统;0--退出");
            int option;
            while(true) // 检验输入的是否为数字,不是则需重新输入
            {
                try
                {
                    option = in.nextInt();
                    break;
                }catch(InputMismatchException e)
                {
                    System.out.println("输入类型不匹配,请重新输入");
                    in=new Scanner(System.in);
                }
            }
            if(option==1)
            {
                work1();
            }else if(option==2)
            {
                work2();
            }else if(option==0)
            {
                break;
            }
        }
    }
    private void work1()
    {
        System.out.println("1、输出日历,格式如下:");
        MyCalendar myCalendar=new MyCalendar(2022,9);
        myCalendar.printCalendar();
    }
    private void work2()
    {
        System.out.println("2、输出如下:");
        System.out.println("************************");
        System.out.println("     人事管理系统");
        System.out.println("     1、添加用户");
        System.out.println("     2、查询用户");
        System.out.println("     3、退出系统");
    }
}