Wednesday , 18 January 2017
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<z; j++) //Adjustment to bring last (6th) row elements to first row
        {
            A[0][j]=A[5][j];
        }    
        
        printCalendar(A, mname, y); //Calling function to print the calendar
    }
    
    //Function for printing the calendar
    void printCalendar(int A[][], String mname, int y)
    {  
        System.out.println("\n\t----------------------------------------------------");
        System.out.println("\t\t\t   "+mname+" "+y); 
        System.out.println("\t----------------------------------------------------"); 
        System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT");
        System.out.println("\t----------------------------------------------------"); 
        
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 7; j++)
            {
                if(A[i][j]!=0)
                    System.out.print("\t "+A[i][j]);
                else
                    System.out.print("\t ");
            }
            System.out.println("\n\t----------------------------------------------------"); 
        }
    }
    
    public static void main(String args[])
    {
        CalendarProgram ob = new CalendarProgram();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the year : ");
        int y = sc.nextInt();
        System.out.print("Enter the month name (e.g. January) : ");
        String mname = sc.next();
        System.out.print("Enter the week day name (e.g. Sunday) of 1st day of "+mname+" : ");
        String wname = sc.next();
        
        int max = ob.findMaxDay(mname,y);
        int f = ob.findDayNo(wname);        
        ob.fillCalendar(max,f,mname,y);
    }
}

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

ISC 2016 Computer Science Solution + Examiner’s Comments – From the Council

Solution of ISC 2016 Computer science Paper as provided by the Council for the Indian School Certificate Examinations.

2 comments

  1. Tarun dey chowdhury

    Very good

  2. Subhrdip mukherjee

    Thank you so much for solving this program.

Leave a Reply

Your email address will not be published. Required fields are marked *