Write a Java Program to Generate One Year Calendar


This Java program displays a calendar for a given year, with each month displayed on a separate page. The program uses a fixed start day of the week for each month, as provided by the startDayOfMonth variable.

The program uses two arrays - months and days - to store the names and number of days for each month. The program first sets the year and start day of the week, and then loops through each month, printing the month name, year, and days of the week header.

The program calculates the number of spaces needed to align the first day of the month with the correct column in the calendar, and then uses a loop to print the dates of the month, with appropriate spacing and line breaks. The program also checks for leap years and adjusts the number of days in February accordingly.

Overall, this program provides a simple and straightforward implementation of a calendar in Java, without relying on external libraries or APIs. However, the fixed start day of the week for each month may not be ideal for some applications, and the program could benefit from additional customization options.

Source Code

package general;
import java.util.Scanner;
public class Display_Calendar
{
	public static void main(String[] args)
	{
		int Y = 2023;
		int startDayOfMonth = 5;
		int spaces = startDayOfMonth;
		String[] months = {
				"",
				"January", "February", "March",
				"April", "May", "June",
				"July", "August", "September",
				"October", "November", "December"
			};
			int[] days = {
				0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
			};
 
			for (int M = 1; M <= 12; M++)
			{
				if  ((((Y % 4 == 0) && (Y % 100 != 0)) ||  (Y % 400 == 0)) && M == 2)
					days[M] = 29;
 
				System.out.println("          "+ months[M] + " " + Y);
				System.out.println("_____________________________________");
				System.out.println("   Sun  Mon Tue   Wed Thu   Fri  Sat");
				   spaces = (days[M-1] + spaces)%7;
				for (int i = 0; i < spaces; i++)
					System.out.print("     ");
				for (int i = 1; i <= days[M]; i++) {
					System.out.printf(" %3d ", i);
					if (((i + spaces) % 7 == 0) || (i == days[M])) 	
						System.out.println();
				}
 
				System.out.println();
		}
	}
}

Output

          January 2023
_____________________________________
   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   29   30
  31

          February 2023
_____________________________________
   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

          March 2023
_____________________________________
   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   29   30   31

          April 2023
_____________________________________
   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   29   30

          May 2023
_____________________________________
   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   29
  30   31

          June 2023
_____________________________________
   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   29   30

          July 2023
_____________________________________
   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   29   30   31

          August 2023
_____________________________________
   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
  29   30   31

          September 2023
_____________________________________
   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   29   30

          October 2023
_____________________________________
   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   29   30
  31

          November 2023
_____________________________________
   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   29   30

          December 2023
_____________________________________
   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   29   30   31

Example Programs