Date conversion program
Hi. Im having trouble with converting the number of days between two dates in my program. Here is the code. Its the birthday and conversion void that im having problems with. The conversion converts the date to the number of days between the current date and 1/11900. The birthday does the same but with 30/12/1987. Please help me sort this out.
publicclass Date{
/**
* Date project
*
* @author Kurt
* @version 1.0, Feb 2007
*/
int m;// Months
int y;// Years
int numDays = 0;//This will store the value of the number of days in a month.
int d;// Days
int daysOver = 0;
public Date(){
d=27;// Fills the 3 ints 1/1/07 initially.
m=2;// This is to avoid having to press add year 2007 times to get to the current year.
y=2007;
switch (m){// Switch statement to declare how many days are in a month.
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((y % 4 == 0) && !(y % 100 == 0))// This section sets up the leap year scenario.
|| (y % 400 == 0) )// Meaning that the Feb has:
numDays = 29;// 29 days on a leap year or
else
numDays = 28;// 28 days normally.
break;
default:
System.out.println("Invalid month.");//Stops the month from going out of the 12 months.
break;
}
}
publicvoid tomorrow(){
d++;// Adds a day, day = day + 1.
if (d > numDays){// Goes forward a month if it reaching the end of the month.
m++;
d = 1;
}
}
publicvoid yesterday(){
d--;// Subtracts a day, day = day - 1.
if (d < numDays){//Goes back a month if the day is bellow 0
m--;
d = numDays;
}
}
publicvoid addYear(){
y++;// adds a year, year = year + 1.
}
publicvoid subtractYear(){
y--;// Subtracts a year, year = year - 1.
}
publicvoid addMonth(){
m++;// Adds a month, month = month + 1.
if (m>12){// If the month goes past December then it goes to January of the next year
y++;
m = 1;
d = 1;
}
}
publicvoid subtractMonth(){
m--;// Subtracts a month, month = month - 1.
if (m < 12){// If the month goes under January then it goes to December of the previous year
y--;
m=12;
d = numDays;
}
}
publicvoid conversion(){// This is the conversion i made to change the date to days over 1/1/1900
d--;
m--;
y = y - 1900;
daysOver =(y*365);// This creates the basic days in a year
daysOver = daysOver - (daysOver/4);//This adds an extra day every 4 years for the leap year day
daysOver = daysOver + d + (numDays*m) ;// This adds the days to the days in the years and then the months past in the year
System.out.println("The number of days after 1900 is"+" "+daysOver);// Prints out the days
}
publicvoid birthday(){// This is the conversion i made to change the date to days over 30/12/1987
d = d - 30;
m = m - 12;
y = y - 1987;
System.out.println("The number of days after 1987 is"+" "+daysOver);// Prints out the days
}
publicvoid reset(){
d = 1;// A section of code to reset the Date to 1/1/2007
m = 1;
y = 2007;
}
publicint getDay(){// Returns the day
return d;
}
publicint getMonth(){// Returns the month
return m;
}
publicint getYear(){// Returns the year
return y;
}
publicvoid output(){// Returns the values or day, month and year as a print out.
System.out.println("The date is "+d+"/"+m+"/"+y);
}
}

