package username.dates; /** * An implementation of dates that stores dates as position within * the year. * * @author Samuel A. Rebelsky * @version 1.0 of September 2005 */ public class DayOfYear implements SimpleDate { // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** The position of the day within the year. */ int days; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Create the date "_month _day". */ public DayOfYear(int _month, int _day) { this.days = 0; for (int month = 0; month < _month; month++) { this.days = this.days + DateHelper.daysInMonth(month); } // for this.days = this.days + _day; } // DayOfYear(int,int) // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Determine this date's position in the year. January 1 is the * 1st day of the year, December 31st is the 365th day of the year. */ public int dayOfYear() { return this.days; } // dayOfYear() /** * Determine the month. Returns a value between 1 and 12. */ public int month() { int tmp = this.days; int month = 1; // Keep checking off months until we reach the correct month while (tmp > DateHelper.daysInMonth(month)) { tmp = tmp - DateHelper.daysInMonth(month); month = month + 1; } // while return month; } // month() /** * Determine the day within the month. */ public int day() { int tmp = this.days; int month = 1; // Keep checking off months until we reach the correct month while (tmp > DateHelper.daysInMonth(month)) { tmp = tmp - DateHelper.daysInMonth(month); month = month + 1; } // while return tmp; } // day() /** * Convert to a string of the form MONTH DAY, such as January 10 * or June 17. */ public String toString() { int tmp = this.days; int month = 1; // Keep checking off months until we reach the correct month while (tmp > DateHelper.daysInMonth(month)) { tmp = tmp - DateHelper.daysInMonth(month); month = month + 1; } // while return DateHelper.nameOfMonth(month) + " " + tmp; } // toString() } // class DayOfYear