public class Date { private int day; private int month; private int year; // Constructor public Date(int m, int d, int y) { day = d; month = m; year = y; } // Returns the Date obtained by adding nDays to the receiving // Date object. public Date add(int nDays) { Date d1 = new Date(month,day,year); for (int i=0; i < nDays; i++) d1 = d1.nextDay(); return d1; } // Returns the Date obtained by subtracting nDays from the receiving // Date object. public Date sub(int nDays) { Date d1 = new Date(month,day,year); for (int i=0; i < nDays; i++) d1 = d1.previousDay(); return d1; } // Returns the number of days between d and the receiving Date object public int daysBetween(Date d) { Date eDate, lDate; if (this.equals(d)) return 0; else { if (this.before(d)) { eDate = this; lDate = d; } else { eDate = d; lDate = this; } int count = 0; for (Date d1 = eDate; eDate.before(lDate); eDate = eDate.nextDay()) count++; return count; } } // Returns true if the receiving Date object is after Date d. public boolean after(Date d) { if (this.year > d.year) return true; else if (this.year < d.year) return false; else if (this.month > d.month) return true; else if (this.month < d.month) return false; else if (this.day > d.day) return true; else if (this.day < d.day) return false; else return false; } // Returns true if the receiving Date object is same as Date d. public boolean equals(Date d) { return ((this.year == d.year) && (this.month == d.month) && (this.day == d.day)); } // Returns true if the receiving Date object is before Date d. public boolean before(Date d) { return !(this.equals(d) || this.after(d)); } // Returns the Date object corresponding to the previous day for the // receiving Data object. public Date previousDay() { int d=0,m=0,y=0; Date yesterday=new Date(m,d,y); if (day > 1) { d = day - 1; m = month; y = year; yesterday = new Date(m,d,y); return yesterday; } else { switch (month) { case 2: case 4: case 6: case 8: case 9: case 11: d = 31; m = month - 1; y = year; break; case 5: case 7: case 10: case 12: d = 30; m = month - 1; y = year; break; case 1: d = 31; m = 12; y = year-1; break; case 3: if (!leapYear(year)) { d = 28; m = 2; y = year; } else { d = 29; m = 2; y = year; } break; } yesterday = new Date(m,d,y); return yesterday; } } // Returns the Date object corresponding to the first day of the // previous month for the receiving Data object. public Date firstOfPreviousMonth() { Date d; if (month > 1) d = new Date(month-1,1,year); else d = new Date(12,1,year-1); return d; } // Returns the Date object corresponding to the next day for the // receiving Data object. public Date nextDay() { int d=0,m=0,y=0; Date tomorrow=new Date(m,d,y); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: if (day < 31) { d = day + 1; m = month; y = year; } else { d = 1; m = month + 1; y = year; } break; case 4: case 6: case 9: case 11: if (day < 30) { d = day + 1; m = month; y = year; } else { d = 1; m = month + 1; y = year; } break; case 12: if (day < 31) { d = day + 1; m = month; y = year; } else { d = 1; m = 1; y = year+1; } break; case 2: if (((day == 28) && !leapYear(year)) || ((day == 29) && leapYear(year)) ) { d = 1; m = month+1; y = year; } else { d = day + 1; m = month; y = year; } break; } tomorrow = new Date(m,d,y); return tomorrow; } // Returns the Date object corresponding to the first day of the // next month for the receiving Data object. public Date firstOfNextMonth() { Date d; if (month < 12) d = new Date(month+1,1,year); else d = new Date(1,1,year+1); return d; } // Pretty print method public String toString() { String sMonths = "01January:02February:03March:04April:05May:06June:" + "07July:08August:09September:10October:11November:12December:"; String m; if (month < 10) m = "0"+month; else m = ""+month; int i = sMonths.indexOf(m); int j = sMonths.indexOf(":",i+1); String sMonth = sMonths.substring(i+2,j); return day + " " + sMonth + ", " + year; } private static boolean leapYear(int year) { return ((year%4 == 0) && !(year%100 == 0 && year%400 != 0)); } }