public class Day { private int year; private int month; private int day; // Constructors public Day(int y, int m, int d) { this.year = y; this.month = m; this.day = d; } public Day(int y) { this(y, 1, 1); } public Day(int y, int nth_dayInYear) { this.year = y; int dPassed = 0; int m = 1; while (dPassed + getMonthTotalDays(y, m) < nth_dayInYear) { dPassed += getMonthTotalDays(y, m); m++; } this.month = m; this.day = nth_dayInYear - dPassed; } private static int getMonthTotalDays(int y, int m) { int d = 31; while (valid(y, m, d) == false) { d--; } return d; } // 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; } // 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 day + " " + MonthNames[month - 1] + " " + year; } // check if the current object is the end of a month public boolean isEndOfAMonth() { if (valid(year, month, day + 1)) // A smart methd: check whether (year month [day+1]) is still a valid date return false; else return true; } // create and return a new Day object which is the "next day" of the current day // object public Day next() { if (isEndOfAMonth()) if (month == 12) return new Day(year + 1, 1, 1); // Since the current day is the end of the year, the next day should be // Jan 01 else return new Day(year, month + 1, 1); // <== your task: first day of next month else return new Day(year, month, day + 1); // <== your task: next day of current month } // create and return a new Day object which is the "next day" of the current day // object public void advance() { if (isEndOfAMonth()) if (month == 12) { year++; month = 1; day = 1; // Since the current day is the end of the year, the next day should be Jan 01 } else { month++; day = 1; } else day++; } }