Java Program To Print Calendar Of Month

Chapter: Interview Programs Last Updated: 13-06-2016 16:33:27 UTC

Program:

            /* ............... START ............... */
                
import java.util.Scanner;

public class JavaPrintCalendarOfMonth {

	// Function to match the given month and return its maximum days
	int findMaxDay(String monthName, 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 (monthName.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 weekName) {
		String days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
		int f = 0;
		for (int i = 0; i < 7; i++) {
			if (weekName.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 monthName, 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, monthName, y); // Calling function to print the
										// calendar
	}

	// Function for printing the calendar
	void printCalendar(int A[][], String monthName, int y) {
		System.out.println("\n\t----------------------------------------------------");
		System.out.println("\t\t\t   " + monthName + " " + 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[]) {
		JavaPrintCalendarOfMonth ob = new JavaPrintCalendarOfMonth();
		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 monthName = sc.next();
		System.out.print("Enter the week day name (e.g. Sunday) of 1st day of " + monthName + " : ");
		String weekName = sc.next();

		int max = ob.findMaxDay(monthName, y);
		int f = ob.findDayNo(weekName);
		ob.fillCalendar(max, f, monthName, y);
	}
}
                /* ............... END ............... */
        

Output

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

	----------------------------------------------------
			   March 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	 30	 31	 	 
	----------------------------------------------------

Tags

Print Calendar Of Month, Java

Similar Programs Chapter Last Updated
Java Program To Find Frequency Of Character In String Interview Programs 28-09-2017
Java Program To Find Power Of Number Using While Loop Interview Programs 30-08-2017
Java Program To Count Divisors Of Integer Number Interview Programs 24-06-2017
Java Program To Sort N Names In Ascending Order Interview Programs 24-06-2017
Java Program To Count Total Number Of Words In String Interview Programs 24-06-2017
Java Program To Print All Prime Numbers From 1 to N Interview Programs 24-06-2017
Java Program To Extract Digits / Numbers From String Interview Programs 22-09-2018
Java First Repeated Character In String Interview Programs 16-05-2017
Java String Character Repetition Count Interview Programs 15-05-2017
Java Program To Check Vowel Or Not Interview Programs 25-09-2018
Java Program To Check Alphabet Or Not Interview Programs 06-04-2017
Java Program To Find First Repeated And Non Repeated Character In String Interview Programs 25-03-2017
Java Spiral Matrix Interview Programs 22-09-2018
Java Program To Reverse A Number Using Strings Interview Programs 13-02-2017
Java Program To Print Diamond Star Pattern Interview Programs 16-12-2016
Java Program To Print Pyramid Pattern Of Star Interview Programs 16-12-2016
Java Program To Find Second Largest Number In Array Interview Programs 04-12-2016
Java Depth First Search Interview Programs 04-12-2016
Java Breadth First Search Interview Programs 04-12-2016
Java Linked List Length Recursive Solution Interview Programs 17-11-2016
Java Linked List Length Iterative Solution Interview Programs 17-11-2016
Java Linked List Node Deletion At Given Position Interview Programs 17-11-2016
Java Linked List Node Delete Interview Programs 17-11-2016
Java Sum Of Digits Using Recursion Interview Programs 06-11-2016
Java Program To Reverse Vowels Of String Interview Programs 05-11-2016
Java Program To Remove Vowels From String Interview Programs 05-11-2016
Java Find Top Two Maximum Numbers In Array Interview Programs 05-11-2016
Java QuickSort Example Interview Programs 05-11-2016
Java Binary Tree Spiral Level Traversal Interview Programs 04-11-2016
Java Binary Tree Preorder Traversal Interview Programs 04-11-2016

1 2 3 4 5