// Computer the number of days in a user-specified month
import java.util.*;
public class DaysInAMonth {
// main application entry point
public static void main(String[] args) {
final int January = 1; final int July = 7;
final int February = 2; final int August = 8;
final int March = 3; final int September = 9;
final int April = 4; final int October = 10;
final int May = 5; final int November = 11;
final int June = 6; final int December = 12;
final int CALENDAR_START = 1852;
// get an input reader
Scanner stdin = new Scanner(System.in);
// get date information of interest
System.out.println("");
System.out.print(" Enter year; ");
int year = stdin.nextInt();
System.out.print(" Enter month numeric value: ");
int month = stdin.nextInt();
// Test input
if ((year < CALENDAR_START) || (month < 1) || ( month > 12)) {
System.out.println("Bad Request;" + year + " "
+ month);
System.exit(1);
}
// determine if year is a leap year
boolean isDivisableBy4 = (year % 4) == 0;
boolean isDivisableBy100 = (year % 100) == 0;
boolean isDivisableBy400 = (year % 400) == 0;
boolean isLeapYear = isDivisableBy4
&& ((! isDivisableBy100) || isDivisableBy400);
// use month and tear information to determine the number of days
int days;
switch (month){
case January: case March: case May: case July: case August:
case October: case December:
days = 31;
break;
case April: case June: case September: case November:
days = 30;
case February:
days = isLeapYear ? 29 : 28;
break;
default:
days = 0; }
System.out.println("");
System.out.println("Month " + month + " in year " + year
+ " has " + days + " days");
}
}