Creating a Yearly Calendar in Java: A Step-by-Step Guide

In this blog, we will delve into the process of creating a yearly calendar using the Java programming language. We will be utilizing various Java libraries, such as the Calendar and SimpleDateFormat classes, to create a calendar that displays all the months of a year in a clear and organized manner. The goal of this tutorial is to provide a step-by-step guide on how to implement a yearly calendar in Java, allowing developers to easily create their own calendars for their projects
  • Create a new Java class and name it "PrintCalendar".
  • Import the necessary libraries, such as "java.util.Calendar" and "java.text.SimpleDateFormat".
  • In the main method, create a new instance of the Calendar class and set it to the current date and time.
  • Use the SimpleDateFormat class to format the date and time in the desired format, such as "MMMM yyyy" for a complete year calendar.
  • Use a for loop to iterate through the entire year, starting from the current month and ending on the last month of the year.
  • In each iteration of the loop, update the Calendar instance to the next month and use the SimpleDateFormat class to print the formatted date and time.
  • After the loop has completed, add a line break to separate the months and print the complete year calendar.
  • Test the program by running it and check the output for the complete year calendar.

package com.calendar;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class PrintCalendar {
    
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
        for (int i = 0; i < 12; i++) {
            calendar.set(Calendar.MONTH, i);
            System.out.println(dateFormat.format(calendar.getTime()));
            System.out.println("Su Mo Tu We Th Fr Sa");
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            int firstDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int j = 0; j < firstDay; j++) {
                System.out.print("   ");
            }
            for (int j = 1; j <= lastDay; j++) {
                System.out.printf("%2d ", j);
                if ((j + firstDay) % 7 == 0) {
                    System.out.println();
                }
            }
            System.out.println();
        }

    }

}

The code above starts by importing the necessary classes: Calendar and SimpleDateFormat. The Calendar class is used to represent a specific date and time, while the SimpleDateFormat class is used to format the date and time in a specific way.

Next, a Calendar object is created and set to the current year. This is done by calling the Calendar.getInstance() method to get a calendar instance, then calling the set() method to set the year to the current year, which is obtained by calling Calendar.getInstance().get(Calendar.YEAR).

A SimpleDateFormat object is also created to format the dates. This is done by creating an object and passing the format string "MMMM yyyy" to the constructor, which represents the month name and the year.

After that, a loop is created to go through the months of the year, from 0 to 11. In each iteration of the loop, the calendar's month is set to the current iteration, and the month name and year is printed out using the SimpleDateFormat object and getTime() method.

Then, the calendar's day of the month is set to 1, and the first day of the month is obtained by calling the get() method and passing in the Calendar.DAY_OF_WEEK constant, minus 1. This is done because the Calendar class considers Sunday as the first day of the week, while the calendar we are trying to create starts with Monday.

The last day of the month is obtained by calling the getActualMaximum() method and passing in the Calendar.DAY_OF_MONTH constant. This gives us the number of days in the current month.


A nested loop is used to print out the calendar for the current month. The first loop prints out the necessary spaces before the first day of the month, based on the value of firstDay. The second loop prints out the days of the month, starting from 1, and using the printf() method to ensure that the days are always two characters wide, even if the day is only one digit. The loop also checks if the current day is the last day of the week (Sunday) and if so, it starts a new line.

Finally, the code prints a blank line after each month's calendar to separate them.

This is a simple example of how to print a complete year calendar using Java, but it can be customized according to your needs.

Output:

January 2023
Su Mo Tu We Th Fr Sa
 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
.....

Comments