public class Day { private int yyyymmdd; // Constructor public Day(int y, int m, int d) { yyyymmdd = y * 10000 + m * 100 + d; } // Accessor methods public int getDay() { return yyyymmdd % 100; } public int getMonth() { return yyyymmdd % 10000 / 100; } public int getYear() { return yyyymmdd / 10000; } // Return a string for the day like dd MMM yyyy public String toString() { final String[] MonthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; return getDay() + " " + MonthNames[getMonth() - 1] + " " + getYear(); } // check if a given year is a leap year static public boolean isLeapYear(int y) { if (y % 400 == 0) return true; else if (y % 100 == 0) return false; else if (y % 4 == 0) return true; else return false; } // check if y,m,d valid static public boolean valid(int y, int m, int d) { if (m < 1 || m > 12 || d < 1) return false; switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return d <= 31; case 4: case 6: case 9: case 11: return d <= 30; case 2: if (isLeapYear(y)) return d <= 29; else return d <= 28; } return false; } private static int getMonthTotalDays(int y, int m) { int d = 31; while (valid(y, m, d) == false) { d--; } return d; } // create and return a new Day object which is the "next day" of the current day // object public Day previous() { if (getDay() > 1) return new Day(getYear(), getMonth(), getDay() - 1); else if (getMonth() == 1) return new Day(getYear() - 1, 12, 31); else { return new Day(getYear(), getMonth() - 1, getMonthTotalDays(getYear(), getMonth() - 1)); } } }