Java Program to Generate Calendar of Any Year Without calendar.get() Function
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
import java.io.*;
import java.util.Scanner;
public class GFG {
public static void main(String a[])
{
Scanner sc = new Scanner(System.in);
System.out.print( "Enter the year : " );
int yy = sc.nextInt();
System.out.print( "Enter month : " );
int mm = sc.nextInt();
int d = 1 ;
int m = 1 ;
int y = 1 ;
int dy = 1 ;
String day[] = { "SUN" , "MON" , "TUE" , "WED" ,
"THU" , "FRI" , "SAT" };
String month[]
= { "JANUARY" , "FEBRUARY" , "MARCH" ,
"APRIL" , "MAY" , "JUNE" ,
"JULY" , "AUGUST" , "SEPTEMBER" ,
"OCTOBER" , "NOVEMBER" , "DECEMBER" };
int ar[] = { 31 , 29 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
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 ;
}
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 ;
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:
Last Updated :
19 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...