// To accept day number, year, and N and display the corresponding date and future date
// date in words (Q1-2009)
import java.util.* ;
class DateQ1_2009 {
static void validateInput(int d,int y,int n){
if(d<1 || d>366) {
System.out.println("INVALID DAY NUMBER");
System.exit(0);
}
if(y<1000 || y>9999) {
System.out.println("INVALID YEAR");
System.exit(0);
}
if(n<2 || n>100) {
System.out.println("INVALID N VALUE");
System.exit(0);
}
}
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
static void printDate(int dd,int mm,int yy){
String monthOfYear[]={"","January","February","March","April","May","June","July","August",
"September","October","November","December"};
String suffix,date;
if(dd%10==1) suffix="st "; // for days that end with 1
else if(dd%10==2) suffix="nd "; // for days that end with 2
else if(dd%10==3) suffix="rd "; // for days that end with 3
else suffix="th "; // for other days
if(dd>10 && dd<14) suffix="th "; // for days 11, 12, and 13
date=dd+suffix+monthOfYear[mm]+" "+yy; // storing date in full form
System.out.println(date);
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int daynum,dd,mm,yy,n;
System.out.print("DAY NUMBER\t: ");
daynum=sc.nextInt();
System.out.print("YEAR\t: ");
yy=sc.nextInt();
System.out.print("DAY AFTER\t: ");
n=sc.nextInt();
validateInput(daynum,yy,n); // validate input data
dd=daynum;
daysOfMonth[2]+=isLeap(yy); // check for days in February
// converts input day number into day, month, and year
for(mm=1;mm<=12 && dd>28;mm++)
dd-=daysOfMonth[mm]; // finds the day of the month
System.out.println("\nOUTPUT :");
printDate(dd,mm,yy); // display current date in words
// finds the date after n days
dd+=n; // add working days to the day number
while(dd>28){
dd-=daysOfMonth[mm]; // finds the day of the year
++mm; // update the month
if(mm>12){
yy++; // update year
mm=1; // start from the first month of new year
daysOfMonth[2]+=isLeap(yy);
}
}
System.out.print("\nDATE AFTER "+n+" DAYS\t: ");
printDate(dd,mm,yy); // display future date in words
} // end of main
} // end of class