Tuesday , 10 December 2019
Home » Class 12 » Java program to print the Calendar of any given month

Java program to print the Calendar of any given month

Question:

Write a program to accept the year, month and the weekday name of the 1st day of that month and generate its calendar.

Example :

INPUT :
Year : 2016
Month : February
1st day of February : Monday

OUTPUT :

	---------------------------------
			   February 2016
	---------------------------------
	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	 	 	 	 	 
	---------------------------------

Programming Code:

/**
* The class CalendarProgram inputs a year, month and the weekday name
* of the 1st day of that month and generates its calendar
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class CalendarProgram
{
    //Function to match the given month and return its maximum days
    int findMaxDay(String mname, int y)
    {
        String months[] = {"","January", "February", "March", "April", "May", "June", 
                            "July", "August", "September", "October", "November", "December"};
        int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        
        if((y%400==0) || ((y%100!=0)&&(y%4==0)))
        {
            D[2]=29;
        }
        int max = 0;
        for(int i=1; i<=12; i++)
        {
            if(mname.equalsIgnoreCase(months[i]))
            {
                max = D[i];  //Saving maximum day of given month
            }
        }
        return max;
    }
    
    //Function to match the given weekday name and return its weekday no.
    int findDayNo(String wname)
    {
        String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
                            "Saturday"};
        int f = 0;
        for(int i=0; i<7; i++)
        {
            if(wname.equalsIgnoreCase(days[i]))
            {
                f = i;  //Saving week day no. given day (e.g. '0' for Sunday)
            }
        }
        return f;
    }
    
    //Function for creating the calendar
    void fillCalendar(int max, int f, String mname, int y)
    {
        int A[][] = new int[6][7];
        int x = 1, z = f;
    
        for(int i=0;i<6;i++)
        {
            for(int j=f; j<7; j++)
            {
                if(x<=max)
                {
                    A[i][j] = x;
                    x++;
                }
            }
            f = 0;
        }
        
        for(int j=0; j

Output:

Enter the year : 2016
Enter the month name (e.g. January) : October
Enter the week day name (e.g. Sunday) of 1st day of October : Saturday

	---------------------------------
			   October 2016
	---------------------------------
	SUN	MON	TUE	WED	THU	FRI	SAT
	---------------------------------
	 30	 31	 	 	 	 	 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
	---------------------------------

Check Also

Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

3 comments