Lecture Videos
  1  import java.util.Scanner;
  2  
  3  public class PrintCalendar {
  4    /** Main method */
  5    public static void main(String[] args) {
  6      Scanner input = new Scanner(System.in);
  7  
  8      // Prompt the user to enter year
  9      System.out.print("Enter full year (e.g., 2001): ");
 10      int year = input.nextInt();
 11  
 12      // Prompt the user to enter month
 13      System.out.print("Enter month in number between 1 and 12: ");
 14      int month = input.nextInt();
 15  
 16      // Print calendar for the month of the year
 17      printMonth(year, month);
 18    }
 19  
 20    /** Print the calendar for a month in a year */
 21    public static void printMonth(int year, int month) {
 22      // Print the headings of the calendar
 23      printMonthTitle(year, month);
 24  
 25      // Print the body of the calendar
 26      printMonthBody(year, month);
 27    }
 28  
 29    /** Print the month title, e.g., May, 1999 */
 30    public static void printMonthTitle(int year, int month) {
 31      System.out.println("         " + getMonthName(month)
 32        + " " + year);
 33      System.out.println("-----------------------------");
 34      System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
 35    }
 36  
 37    /** Get the English name for the month */
 38    public static String getMonthName(int month) {
 39      String monthName = "";
 40      switch (month) {
 41        case 1: monthName = "January"; break;
 42        case 2: monthName = "February"; break;
 43        case 3: monthName = "March"; break;
 44        case 4: monthName = "April"; break;
 45        case 5: monthName = "May"; break;
 46        case 6: monthName = "June"; break;
 47        case 7: monthName = "July"; break;
 48        case 8: monthName = "August"; break;
 49        case 9: monthName = "September"; break;
 50        case 10: monthName = "October"; break;
 51        case 11: monthName = "November"; break;
 52        case 12: monthName = "December";
 53      }
 54  
 55      return monthName;
 56    }
 57  
 58    /** Print month body */
 59    public static void printMonthBody(int year, int month) {
 60      // Get start day of the week for the first date in the month
 61      int startDay = getStartDay(year, month);
 62  
 63      // Get number of days in the month
 64      int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
 65  
 66      // Pad space before the first day of the month
 67      int i = 0;
 68      for (i = 0; i < startDay; i++)
 69        System.out.print("    ");
 70  
 71      for (i = 1; i <= numberOfDaysInMonth; i++) {
 72        System.out.printf("%4d", i);
 73  
 74        if ((i + startDay) % 7 == 0)
 75          System.out.println();
 76      }
 77  
 78      System.out.println();
 79    }
 80  
 81    /** Get the start day of month/1/year */
 82    public static int getStartDay(int year, int month) {
 83      final int START_DAY_FOR_JAN_1_1800 = 3;
 84      // Get total number of days from 1/1/1800 to month/1/year
 85      int totalNumberOfDays = getTotalNumberOfDays(year, month);
 86  
 87      // Return the start day for month/1/year
 88      return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
 89    }
 90  
 91    /** Get the total number of days since January 1, 1800 */
 92    public static int getTotalNumberOfDays(int year, int month) {
 93      int total = 0;
 94  
 95      // Get the total days from 1800 to 1/1/year
 96      for (int i = 1800; i < year; i++)
 97        if (isLeapYear(i))
 98          total = total + 366;
 99        else
100          total = total + 365;
101  
102      // Add days from Jan to the month prior to the calendar month
103      for (int i = 1; i < month; i++)
104        total = total + getNumberOfDaysInMonth(year, i);
105  
106      return total;
107    }
108  
109    /** Get the number of days in a month */
110    public static int getNumberOfDaysInMonth(int year, int month) {
111      if (month == 1 || month == 3 || month == 5 || month == 7 ||
112        month == 8 || month == 10 || month == 12)
113        return 31;
114  
115      if (month == 4 || month == 6 || month == 9 || month == 11)
116        return 30;
117  
118      if (month == 2) return isLeapYear(year) ? 29 : 28;
119  
120      return 0; // If month is incorrect
121    }
122  
123    /** Determine if it is a leap year */
124    public static boolean isLeapYear(int year) {
125      return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
126    }
127  }