public class Date {
private int year;
private int month;
private int day;
// Constructor: Date object with year, month and day as parameters.
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public Date() {
// TODO Auto-generated constructor stub
}
// Return year to the date object above
public int getYear() {
return year;
}
// Return month in the date object above
public int getMonth() {
return month;
}
// Return day in the date object above
public int getDay() {
return day;
}
// formats the date to look like; "Year/month/day"
public String toString() {
return (Integer.toString(year) + "/" + Integer.toString(month) + "/" + Integer.toString(day));
}
// compares the 2 dates given to see if they match.
public boolean equals(Date date2) {
return (this.year == date2.getYear() && this.month == date2.getMonth() && this.day == date2.getDay());
}
// uses a boolean method to check if there is a leap year.
public static boolean isLeapYear(int year) {
boolean check = false;
if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0)) {
check = true;
}
return check;
}
// Return the number of days in the month and year
// Uses array lookup plus for February check if it is a leap year.
public static int daysInMonth(int month, int year) {
// days in month table using indexes 1-12
final int monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
if (month == 2 && isLeapYear(year)) // special case for February
return 29;
return monthDays[month];
}
// return the next Date
public Date nextDay() {
Date next = new Date();
next.month = month;
next.day = day;
next.year = year;
if (month == 2)
if (day < 28)
next.day++;
else {
next.day = 1;
next.month = 3;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
if (day < 30)
next.day++;
else {
next.day = 1;
next.month = month++;
}
else if (month == 12 && day == 31) {
next.month = 1;
next.day = 1;
next.year++;
} else if (day < 31)
next.day++;
else {
next.day = 1;
next.month = month++;
}
return next;
}
}
public class DateTester {
public static void main(String[] args) {
// declares all the variables that will be used.
Date today = new Date(5,1,2012);
Date myBirthday = new Date(1,23,1987);
Date partnerBirthday = new Date(1,11,1785);
Date xmas = new Date(12,25,2012);
// prints out all the info above^^^/>^^
System.out.println("Today is " + today);
System.out.println("Phil's birthday is " + myBirthday);
System.out.println("Partner's birthday is " + partnerBirthday);
System.out.println("Xmas is " + xmas);
System.out.println();
// adds a new variable and displays a date comparison
Date today2 = new Date(5,2,2012); // use today's date
System.out.println(today + " equals " + today2 + " " + today.equals(today2) );
System.out.println(today + " equals " + xmas + " " + today.equals(xmas) );
System.out.println();
// print statements that show if a specific year is a leapyear.
System.out.println("Is 2000 a leap year? " + Date.isLeapYear(2000) );
System.out.println("Is 2001 a leap year? " + Date.isLeapYear(2001) );
System.out.println("Is 1900 a leap year? " + Date.isLeapYear(1900) );
System.out.println("Is 2008 a leap year? " + Date.isLeapYear(2008) );
System.out.println();
// print 10 days starting with xmas
Date date = xmas;
for (int i = 1; i <= 10; i++) {
System.out.println(i + ". " + date);
date = date.nextDay();
}
}
}
And this is the ouput im getting:
Today is 5/1/2012
Phil's birthday is 1/23/1987
Partner's birthday is 1/11/1785
Xmas is 12/25/2012
5/1/2012 equals 5/2/2012 false
5/1/2012 equals 12/25/2012 false
Is 2000 a leap year? true
Is 2001 a leap year? false
Is 1900 a leap year? false
Is 2008 a leap year? true
1. 12/25/2012 // the days are supposed to increase 10 times with the month and year rolling over as well.
2. 12/25/1
3. 12/25/2
4. 12/25/3
5. 12/25/4
6. 12/25/5
7. 12/25/6
8. 12/25/7
9. 12/25/8
10. 12/25/9

New Topic/Question
Reply



MultiQuote




|