import java.util.Calendar;

/*************************************************************************
 * Compilation: javac Calendar.java Execution: java Calendar M Y
 * 
 * This program takes the month M and year Y and prints a calendar for that
 * month.
 * 
 * % java Calendar 7 2005 July 2005 S M T W Th F S 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
 * 
 *************************************************************************/
public class MyCal {
	/**********************************************************************
	 * Given the month (M), day (D), and year (Y), return which day of the week
	 * it falls on according to the Gregorian calendar. For M use 1 for January,
	 * 2 for February, and so forth. Outputs 0 for Sunday, 1 for Monday, and so
	 * forth.
	 **********************************************************************/
	public static int day(int M, int D, int Y) {
		int y = Y - (14 - M) / 12;
		int x = y + y / 4 - y / 100 + y / 400;
		int m = M + 12 * ((14 - M) / 12) - 2;
		int d = (D + x + (31 * m) / 12) % 7;
		return d;
	}

	public static int[] monthinfo(Calendar cal) {
		int[] days = new int[4]; // weekday, # of days in month, next day
		int k;
		k = cal.get(Calendar.DAY_OF_WEEK);
		days[0] = (k == 7) ? 0 : k - 1;
		days[1] = cal.getMaximum(Calendar.DAY_OF_MONTH);
		cal.roll(Calendar.MONDAY, 1);
		k = cal.get(Calendar.DAY_OF_WEEK);
		days[2] = (k == 7) ? 0 : k - 1;
		days[3] = cal.getMaximum(Calendar.DAY_OF_MONTH);
		return days;
	}

	// return true if the given year is a leap year
	public static boolean isLeapYear(int year) {
		return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
	}

	public static void main(String[] args) {
		// int M = Integer.parseInt(args[0]); // month (Jan = 1, Dec = 12)
		// int Y = Integer.parseInt(args[1]); // year
		String[] mpr = new String[8];
		Calendar cal = Calendar.getInstance();

		int Y = cal.get(Calendar.YEAR);

		int M = cal.get(Calendar.MONTH) + 1;
		cal.set(Y, M - 1, 1);
		int[] minfo = monthinfo(cal);
		for (int d : minfo) {
			System.out.printf("%d ", d);
		}

		// months[i] = name of month i
		String[] months = {
				"", // leave empty so that months[1] = "January"
				"January", "February", "March", "April", "May", "June", "July",
				"August", "September", "October", "November", "December" };

		// days[i] = number of days in month i
		int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		// check for leap year
		if (M == 2 && isLeapYear(Y))
			days[M] = 29;

		// print calendar header
		System.out.println("   " + months[M] + " " + Y);
		System.out.println(" S  M Tu  W Th  F  S");

		// starting day
		int d = day(M, 1, Y);

		// print the calendar
		for (int k = 0; k < d; k++)
			System.out.print("   ");
		for (int k = 1; k <= days[M]; k++) {
			System.out.printf("%2d ", k);
			if (((k + d) % 7 == 0) || (k == days[M]))
				System.out.println();
		}

	}
}
