Write a Java program to generate the calendar for the year 2
Write a Java program to generate the calendar for the year 2017 (Jan – Dec). The program should prompt the user to input the year for the calendar of interest. The progam must check for leap years and make the number of days 29 for the month of February. The output must show the year and your week may start on Sunday and end on Saturday. See sample below:
2017 Calendar
January 2017
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
Solution
import java.util.Scanner;
 public class Calender {
public static int day(int month, int day, int year) {
 int y = year - (14 - month) / 12;
 int x = y + y/4 - y/100 + y/400;
 int m = month + 12 * ((14 - month) / 12) - 2;
 int d = (day + x + (31*m)/12) % 7;
 return d;
 }
// check leap year
 public static boolean checkLeap(int year) {
 if ((year % 4 == 0) && (year % 100 != 0)) return true;
 if (year % 400 == 0) return true;
 return false;
 }
public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print(\"Enter a year:\");
 int year= sc.nextInt();
String[] month_names = {
 \"\",   
 \"January\", \"February\", \"March\",
 \"April\", \"May\", \"June\",
 \"July\", \"August\", \"September\",
 \"October\", \"November\", \"December\"
 };
// days_of_month[i] = number of days_of_month in month i
 int[] days_of_month = {
 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
 };
for(int j=0;j<12;j++){
    int month = j;
    // check for leap year
 if (month == 2 && checkLeap(year)) days_of_month[month] = 29;
 // print header
 System.out.println(\" \" + month_names[month] + \" \" + year);
 System.out.println(\" S M Tu W Th F S\");
// starting day
 int d = day(month, 1, year);
// print the calendar
 for (int i = 0; i < d; i++)
 System.out.print(\" \");
 for (int i = 1; i <= days_of_month[month]; i++) {
 System.out.printf(\"%2d \", i);
 if (((i + d) % 7 == 0) || (i == days_of_month[month])) System.out.println();
 }
 System.out.println();
 }
 }
 }


