Java Program to Generate Calendar of Any Year Without calendar.get() Function
Last Updated :
25 Apr, 2025
Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part.
Illustration:
Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers and the output would return the desired month's calendar for the respective year in a proper format.
Procedure:
Step 1: Take the year and the month as integer inputs from the user
Step 2: Create 2 arrays as follows, one for storing days and the other for storing the months, as per proper order.
String day[] = { "SUN","MON","TUE","WED","THU","FRI","SAT" } ;
String month[] = { "JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER" } ;
Step 3: Initialize a counter variable and three variables, each for the day, month, and year as 1, and a separate array for storing the different combinations of days on which months can be found. Eg. 31,30,29
int ar[] = { 31,29,31,30,31,30,31,31,30,31,30,31 } ;
Step 4: Check the leap year condition and re-initialize values for the above array.
if(y%4==0&&y%100!=0||y%100==0)
ar[1]=29; // if the year is a leap year then store 29 for the month of february
else
ar[1]=28; // else 28
Step 5: Increment year count as month count reaches 12 and increment month count as day count reaches a value greater than that present in the array for the respective index
Step 6: Print the result.
Implementation:
Example
Java
// Java Program to Generate Desired Calendar
// Without calendar.get() function or
// Inputting the Year and the Month
// Importing required classes
import java.io.*;
import java.util.Scanner;
// Main class
public class GFG {
// Main driver method
public static void main(String a[])
{
// Reading input by creating object of Scanner class
Scanner sc = new Scanner(System.in);
// Display message only
System.out.print("Enter the year : ");
// Reading integer input value
int yy = sc.nextInt();
// Display message only
System.out.print("Enter month : ");
// Reading integer input value
int mm = sc.nextInt();
int d = 1;
int m = 1;
int y = 1;
int dy = 1;
// Storing data and months as input
String day[] = { "SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT" };
String month[]
= { "JANUARY", "FEBRUARY", "MARCH",
"APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER",
"OCTOBER", "NOVEMBER", "DECEMBER" };
// Custom array as input
int ar[] = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
// Till condition holds true
while (true) {
if (d == 1 && m == mm && y == yy) {
break;
}
if (y % 4 == 0 && y % 100 != 0
|| y % 100 == 0) {
ar[1] = 29;
}
else {
ar[1] = 28;
}
dy++;
d++;
if (d > ar[m - 1]) {
m++;
d = 1;
}
if (m > 12) {
m = 1;
y++;
}
if (dy == 7) {
dy = 0;
}
}
int c = dy;
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
ar[1] = 29;
}
else {
ar[1] = 28;
}
// Print the desired month of input year
System.out.println("MONTH:" + month[mm - 1]);
for (int k = 0; k < 7; k++) {
System.out.print(" " + day[k]);
}
System.out.println();
for (int j = 1; j <= (ar[mm - 1] + dy); j++) {
if (j > 6) {
dy = dy % 6;
}
}
int spaces = dy;
if (spaces < 0)
spaces = 6;
// Printing the calendar
for (int i = 0; i < spaces; i++)
System.out.print(" ");
for (int i = 1; i <= ar[mm - 1]; i++) {
System.out.printf(" %4d ", i);
if (((i + spaces) % 7 == 0)
|| (i == ar[mm - 1]))
System.out.println();
}
}
}
Output:Â
Â
Similar Reads
Java Program to Display Dates of a Calendar Year in Different Format
As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a
4 min read
Java Program to Display Name of Months of Calendar Year in Short Format
As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names
3 min read
C program to display month by month calendar for a given year
Prerequisite: Find day of the week for a given date Given a year N, the task is to print the calendar for every month of the given year. Implementation: C// C program to print the month by month // calendar for the given year #include <stdio.h> // Function that returns the index of the // day
4 min read
Java Program to Display Name of the Weekdays in Calendar Year
Concept: In java when it comes down to the date and time problems after hitting the brute force method one should always remember the Date class of java which not only provides to print current or forthcoming year, month, day, date, time, hour, minutes, and even precision to seconds. Not only one ca
5 min read
Java Program to Get Year From Date
Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get a year from date using Java. Methods: The
4 min read
Java Program to Find if a Given Year is a Leap Year
Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java. Facts about Leap YearEvery leap year corresponds to these facts : A century year is a year ending with 00. A century year is a leap year only if it is divisible
5 min read
How to Create a Date Object using the Calendar Class in Java
In Java, the Calendar class can provide a flexible way to handle the dates and times. This article demonstrates how to create the Date object using the Calendar class by setting the specific date and time and converting it to the Date object and printing the exact date and time. This approach offers
2 min read
How to get Day, Month and Year from Date in Java
Given a date in the form of a string, the task is to write a Java Program to get the day, month, and year from the given date. Examples: Input: date = "2020-07-18"Output:Day: 18Month: JulyYear: 2020Explanation: The given date is '2020-07-18', so the day is: 18, the month is: July, and the year is: 2
4 min read
Java Program to Convert Date to String
The date type object can be converted to a string in Java using a large variety of in-built classes available in Java. Given a date, we need to convert the date to a given format of the string. Examples: Input: date = â2020-11-13âOutput: 2020-11-13 Input: date = â2020-12-14âOutput: 2020-12-14 Method
3 min read
Java Program to Print the Months in Different Formats
For printing months in different formats, we are going to use two classes of java.util package. That is the first one Calendar class and another one is Formatter class. From Calendar class use getInstance() method to get instance (time and date information) of calendar according to the current time
2 min read