Display the calendar of a given year and month, implemented in java

1. Structure diagram

Insert picture description here

2. Code

package com.zhuo.base.com.zhuo.base;

import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;

import java.util.Scanner;

public class PrintCalendar {
    
    
    /*main方法*/
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("Enter full year (e.g., 2012): ");//提示用户输入年份
        int year = input.nextInt();
        System.out.print("Enter month as a number between 1 and 12: ");//提示用户输入月份
        int month = input.nextInt();
        printMonth(year, month);//打印一年中某个月的日历调用
    }
    /*打印一年中一个月的日历方法*/
    public static void printMonth(int year, int month) {
    
    
        printMonthTitle(year, month);//打印日历的标题方法调用
        printMonthBody(year, month);//打印日历正文方法调用
    }
    /*打印月份标题方法 */
    public static  void printMonthTitle(int year, int month) {
    
    
        System.out.println("           " + getMonthName(month) + " " + year);
        System.out.println(" ---------------------------");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }
    /*获取月份的英文名称方法 */
    public static String getMonthName(int month) {
    
    
        String monthName = "";
        switch (month) {
    
    
            case 1: monthName = "January"; break;
            case 2: monthName = "February"; break;
            case 3: monthName = "March"; break;
            case 4: monthName = "April"; break;
            case 5: monthName = "May"; break;
            case 6: monthName = "June"; break;
            case 7: monthName = "July"; break;
            case 8: monthName = "August"; break;
            case 9: monthName = "September"; break;
            case 10: monthName = "October"; break;
            case 11: monthName = "November"; break;
            case 12: monthName = "December";
        }
        return monthName;
    }
    /*打印月体方法*/
    public static void printMonthBody(int year, int month) {
    
    
        int startDay = getStartDay(year, month);//为每月的第一个天获取一周的开始日期方法调用
        int numOfDayInMonth = getNumberOfDaysInMonth(year, month);//获取当月天数方法调用
        /* 在每月第一天之前填充空间*/
        int i = 0;
        for (i = 0; i < startDay; i++)
            System.out.print("    ");
        for (i = 1; i <= numOfDayInMonth; i++) {
    
    
            System.out.printf("%4d", i);
            if ((i + startDay) % 7 == 0)
                System.out.println();
        }
        System.out.println();
    }
    /*获取每一年每个月开始日期*/
    public static int getStartDay(int year, int month) {
    
    
        final int START_DAY_FOR_JAN_1_1800 = 3;
        int totalNumberOfDays = getTotalNumberOfDays(year, month);//获取从1800年1月1日到每一年每个月一号总天数的方法调用
        return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;//返回每一年每一个月的开始日期
    }
    /*获取自1800年1月1日以来的总天数的方法 */
    public static int getTotalNumberOfDays(int year, int month) {
    
    
        int total = 0;
        /*得到从1800年1月1号到任何一年的总天数*/
        for (int i = 1800; i < year; i++)
            if (isLeapYear(i))
                total += 366;
            else
                total += 365;
        for (int i = 1; i < month; i++) {
    
    
            total += getNumberOfDaysInMonth(year, i);//添加从一月到日历月前一个月的天数
        }
        return total;
    }
    /*获取一个月的天数的方法*/
    public static int getNumberOfDaysInMonth(int year, int month) {
    
    
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
            return 31;
        else if (month == 4 || month == 6 || month == 9 || month == 11)
            return 30;
        else
            return isLeapYear(year) ? 29 : 28;
    }
    /*确定是否是闰年的方法*/
    public static boolean isLeapYear(int year) {
    
    
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}

Three. Results display

Enter full year (e.g., 2012): 2021
Enter month as a number between 1 and 12: 2
           February 2021
 ---------------------------
 Sun Mon Tue Wed Thu Fri Sat
       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

Process finished with exit code 0

4. Top-down design

First divide the problem into two sub-problems: read user input and print the month's calendar.

  1. Use Scanner to read the input of the year and month.
  2. The problem of printing a calendar for a given month can be broken down into two sub-problems: printing the title of the calendar and the main body of the calendar
  • The title of the monthly calendar consists of three lines: the year and month, the dotted line, and the name of the week of the seven days of the week. The full name of the month (for example: January) needs to be determined by the number that represents the month (for example: 1). This step is done by getMonthName
  • In order to print the main body of the calendar, you need to know the first day of the month is the day of the week (getStartDay) and how many days there are in the month (getNumberOfDaysInMonth), for example: December 2013 has 31 days, and December 1, 2013 is Sunday.
  1. How can I know what day is the first day of a month? There are several ways to get it. The following method is used here. Suppose we know that January 1, 1800 is Wednesday (START_DAY_F0R_JAN_l_1800=3), and then calculate the total number of days (totalNumberOfDays) between January 1, 1800 and the first day of the calendar month. Since there are 7 days in each week, the week of the first day of the calendar month is (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7. In this way, the problem of getStartDay can be further refined into getTotalNumberOfDays.
  2. To calculate the total number of days, you need to know whether the year is a leap year and the number of days in each month. Therefore, getTotalNumberOfDays can be further refined into two sub-problems: isLeapYear and getNumberOfDaysInMonth.

Five. Implementation details

The method isLeapYear(int year) can be implemented with the following code:
return year% 400 == 0 || (year% 4 == 0 && year% 100 != 0);
Use the following facts to implement the getTotalNumberOfDaysInMonth(intyear.intmonth) method:
-Months, March, May, July, August, October and December have 31 days. There are 30 days in April, June, September and November. February usually has 28 days, but there are 29 days in a leap year. Therefore, a year usually has 365 days, and a leap year has 366 days.
Implement the getTotalNumberOfDays(int year, int month) method: you
need to calculate the total number of days (totalNumberOfDays) between January 1, 1800 and the first day of the calendar month. You can find the total number of days from 1800 to the calendar year, and then find the total number of days before the calendar month in the calendar year. The sum of these two total days is
totalNumberOfDays.
To print the calendar body, first fill in some spaces before the first day, and then print a line for each week.

Guess you like

Origin blog.csdn.net/weixin_42768634/article/details/113643856