// Programmer: Rance Cleaveland // Date: 9 Oct. 2006 // // This class implements simple operations on dates. public class Date { public static int DEFAULT_DAY = 1; // Default day value public static int DEFAULT_MONTH = 1; // Default month value public static int DEFAULT_YEAR = 1900; // Default year value public static String DEFAULT_SEPARATOR = "/"; // Default separator public static String DEFAULT_NOTE = ""; // Default note private int day = DEFAULT_DAY; // Day part of date private int month = DEFAULT_MONTH; // Month part of date private int year = DEFAULT_YEAR; // Year part of date private String separator = DEFAULT_SEPARATOR; // Separator for date private String note = DEFAULT_NOTE; // Comment field for date // Default constructor Date () { } // Another constructor. Consistency check ensures only valid data returned // (erroneous arguments lead to default constructor being applied) Date (int newDay, int newMonth, int newYear){ day = newDay; month = newMonth; year = newYear; if (!(isValid (this))) { System.out.println ("Invalid arguments to Date constructor: " + newDay + "," + newMonth + "," + newYear); restoreDefaults (); } } // Copy constructor Date (Date d) { day = d.day; month = d.month; year = d.year; separator = d.separator; note = d.note; } // restoreDefaults restores default values for all fields public void restoreDefaults () { day = DEFAULT_DAY; month = DEFAULT_MONTH; year = DEFAULT_YEAR; separator = DEFAULT_SEPARATOR; note = DEFAULT_NOTE; } // isValid determines if Date is valid (year is non-negative, month falls in 1-12, // and day is correct). public static boolean isValid (Date d) { if (d.year < 0) return false; else if (d.month < 1) return false; else if (d.month > 12) return false; else if (d.day < 1) return false; else if (d.day > d.numDaysInMonth ()) return false; else return true; } // The method for converting dates to strings public String toString () { return (month + separator + day + separator + year); } // Method for printing dates to System.out public void print () { System.out.print (toString ()); } // Method for printing dates plus surrounding text public void print (String pre, String post){ System.out.print (pre); print (); System.out.print (post); } // Method for printing dates followed by carriage return public void println () { print (); System.out.println (); } // Method for printing dates plus surrounding text, followed by return public void println (String pre, String post) { print (pre, post); System.out.println (); } // "incrementYear" increments the year. public void incrementYear () { year++; } // "incrementMonth" increments the month. public void incrementMonth () { if (month < 12) month++; else { month = 1; year++; } } // "setYear" allows the year to be set to a give value public void setYear (int newYear) { int oldYear = year; year = newYear; if (!(Date.isValid (this))){ System.out.println ("Invalid year argument to setYear: " + newYear); year = oldYear; // Restore old year value } } // "setMonth" allows the month to be set; an error is reported // if the month is invalid public void setMonth (int newMonth) { if ((1 <= newMonth) && (newMonth <= 12)) month = newMonth; else System.out.println ("Bad argument to setMonth: " + newMonth); } // "isLeapYear" returns true if the year is a leap year. public boolean isLeapYear () { return ((year % 4) == 0 && ((year % 100 != 0) || (year % 400 == 0))); } // "numDaysInMonth" returns the number of days in the current month private int numDaysInMonth () { if (month == 1) return 31; else if (month == 2) { if (isLeapYear ()) return 29; else return 28; } else if (month == 3) return 31; else if (month == 4) return 30; else if (month == 5) return 31; else if (month == 6) return 30; else if (month == 7) return 31; else if (month == 8) return 31; else if (month == 9) return 30; else if (month == 10) return 31; else if (month == 11) return 30; else // (month == 12) return 31; } // "setDay" allows the day to be set; an error is reported if the // day value is invalid for the given month public void setDay (int newDay) { if (numDaysInMonth () >= newDay) day = newDay; else System.out.println ("Invalid number of days (month = " + month +"): " + newDay); } // "addDays" adds given number of days to date (must be positive) public void addDays (int future) { if (future < 0) System.out.println("Invalid argument to addDays: " + future); else { int daysLeft = day + future; while (daysLeft > numDaysInMonth()) { daysLeft -= (numDaysInMonth ()); incrementMonth (); } day = daysLeft; } } // "getDay" returns the day public int getDay () { return day; } // "getMonth" returns the month public int getMonth () { return month; } // "getYear" returns the year public int getYear () { return year; } // "isValentinesDay" returns true if the date is Valentines Day public boolean isValentinesDay () { return ((month == 2) && (day == 14)); } // "equals" computes an equality function on dates public boolean equals (Date d) { return (day == d.day && month == d.month && year == d.year); } // "isBefore" returns true if the date is before the inputted date public boolean isBefore (Date d) { if (year < d.year) return true; else if (year == d.year) { if (month < d.month) return true; else if ( (month == d.month) && (day < d.day) ) return true; else return false; } else return false; } }