public class Date { private static int[] days_of_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private static String[] month_name = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; private int year; private int month; private int day; public Date(int year, int month, int day) { if(month < 0 || month >= days_of_month.length) { throw new IllegalArgumentException("Invalid month number " + month); } this.year = year; this.month = month; this.day = day; if(daysLeftInMonth() < 0) { throw new IllegalArgumentException("Invalid day number " + day + " in " + month_name[month]); } } public boolean equals(Date other) { return this.year == other.year && this.month == other.month && this.day == other.day; } public void increment(int days) { while(days >= daysLeftInMonth()) { days -= daysLeftInMonth(); month++; if(month == days_of_month.length) { year++; month = 0; } day = 1; } day += days; } public String toString() { return day + " " + month_name[month] + " " + year; } private int daysLeftInMonth() { if(isLeapYear() && month == 1) { return days_of_month[month] - day + 2; } else { return days_of_month[month] - day + 1; } } private boolean isLeapYear() { if(year % 400 == 0) return true; if(year % 100 != 0) return false; if(year % 4 == 0) return true; return false; } }