import java.util.Scanner;
public class CalculateDates{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a year (E.g.: 2013): ");
int year = input.nextInt();
System.out.print("Please enter a month (1 = January, 12 = December): ");
int month = input.nextInt();
System.out.print("Please enter a day (1-31): ");
int day = input.nextInt();
validateInput(year,month,day);
}
public static void validateInput(int year, int month, int day){
if(isLeapYear(year) && month == 2 && day > 29) {
System.out.println("Invalid input.");
System.out.println("Aborting..");
System.exit(0);
} else if(isLeapYear(year) == false && month == 2 && day > 28) {
System.out.println("Invalid input.");
System.out.println("Aborting..");
System.exit(0);
} else if(month > 12 || month == 1 && day > 31 || month == 3 && day > 31 || month == 4 && day > 30 || month == 5 && day > 31 || month == 6 && day > 30 || month == 7 && day > 31 || month == 8 && day > 31 || month == 9 && day > 30 || month == 10 && day > 31 || month == 11 && day > 30 || month == 12 && day > 31) {
System.out.println("Invalid input.");
System.out.println("Aborting..");
System.exit(0);
}
daysInMonth(month,year);
getMonthName(month);
System.out.println(computeTotalDays(year,month,day)+" days have passed since the beginning of the year.");
System.out.print("The next day is ");
nextDay(year,month,day);
return;
}
public static String getMonthName(int month){
String month_name = new String();
switch(month){
case 1: month_name = "January"; break;
case 2: month_name = "February"; break;
case 3: month_name = "March"; break;
case 4: month_name = "April"; break;
case 5: month_name = "May"; break;
case 6: month_name = "June"; break;
case 7: month_name = "July"; break;
case 8: month_name = "August"; break;
case 9: month_name = "September"; break;
case 10: month_name = "October"; break;
case 11: month_name = "November"; break;
case 12: month_name = "December"; break;
default: month_name = "January"; break;
}
return month_name;
}
public static boolean isLeapYear(int year){
boolean day;
if(year%4 == 0 && year%100 != 0){
day = true;
}else if(year%400 == 0){
day = true;
}else{
day = false;
}
return day;
}
public static int daysInMonth(int month, int year){
int day = 0;
int i;
for(i = 1; i < month; i++){
switch(i){
case 1: day += 31; break;
case 2: if(isLeapYear(year)) day += 29; else day += 28; break;
case 3: day += 31; break;
case 4: day += 30; break;
case 5: day += 31; break;
case 6: day += 30; break;
case 7: day += 31; break;
case 8: day += 31; break;
case 9: day += 30; break;
case 10: day += 31; break;
case 11: day += 30; break;
case 12: day += 31; break;
default: System.out.println("Unknown Error.."); System.exit(0);
}
}
return day;
}
public static int computeTotalDays(int year, int month, int day){
int totaldays = day;
totaldays += daysInMonth(month,year);
return totaldays;
}
public static void nextDay(int year, int month, int day){
int nextday = 0;
int daysinmonth = 0;
switch(month){
case 1: daysinmonth = 31; break;
case 2: if(isLeapYear(year)) daysinmonth = 29; else daysinmonth = 28; break;
case 3: daysinmonth = 31; break;
case 4: daysinmonth = 30; break;
case 5: daysinmonth = 31; break;
case 6: daysinmonth = 30; break;
case 7: daysinmonth = 31; break;
case 8: daysinmonth = 31; break;
case 9: daysinmonth = 30; break;
case 10: daysinmonth = 31; break;
case 11: daysinmonth = 30; break;
case 12: daysinmonth = 31; break;
default: System.out.println("Unknown Error.."); System.exit(0);
}
if(month == 12 && (daysinmonth-day) == 0)
year++;
if((daysinmonth-day) == 0){
month++;
getMonthName(month);
day = 1;
}else{
day++;
}
System.out.println(getMonthName(month)+" "+day+", "+year);
}
}