// Declaration of the MyDate class.

import java.text.DecimalFormat;
import java.io.Serializable;


public class MyDate extends Object implements Serializable
{
    private int month;  // 1-12
    private int day;    // 1-31 based on month
    private int year;   // any year

    // Constructor: Confirm proper value for month;
    // call method checkDay to confirm proper
    // value for day.
    public MyDate (int theMonth, int theDay, int theYear)
    {
	if (theMonth > 0 && theMonth <= 12)    // validate month
	    month = theMonth;
	else
	{
	    month = 1;
	    System.out.println ("Month " + theMonth +
		    " invalid. Set to month 1.");
	}

	year = theYear;                 // could validate year
	day = checkDay (theDay);        // validate day


    }


    public void setDate (int theMonth, int theDay, int theYear)
    {
	setDay (theDay);
	setMonth (theMonth);
	setYear (theYear);
    }


    public void setDay (int theDay)
    {
	if (theDay >= 0 && theDay <= 31)
	    day = theDay;
	else
	    day = 1;
	if (month != 0)
	    day = checkDay (day);
    }


    public void setMonth (int theMonth)
    {
	month = (theMonth >= 1 && theMonth <= 12 ? theMonth:
	1);
    }


    public void setYear (int theYear)
    {
	year = (theYear >= 0 ? theYear:
	0);
    }


    public int getQuarter ()
    {
	return month / 4 + 1;
    }


    // Utility method to confirm proper day value
    // based on month and year.
    private int checkDay (int testDay)
    {
	int daysPerMonth [] =
	{
	    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	}
	;

	// check if day in range for month
	if (testDay > 0 && testDay <= daysPerMonth [month])
	    return testDay;

	// check for leap year
	if (month == 2 && testDay == 29 &&
		(year % 400 == 0 ||
		    (year % 4 == 0 && year % 100 != 0)))
	    return testDay;

	System.out.println ("Day " + testDay +
		" invalid. Set to day 1.");

	return 1;  // leave object in consistent state
    }


    public int compareTo (MyDate d)  // see the String.compareTo() method
    {
	DecimalFormat twoDigits = new DecimalFormat ("00");
	DecimalFormat fourDigits = new DecimalFormat ("0000");
	String thisHold = new String (fourDigits.format (year) + twoDigits.format (month) + twoDigits.format (day));
	String otherHold = new String (fourDigits.format (d.year) + twoDigits.format (d.month) + twoDigits.format (d.day));
	return thisHold.compareTo (otherHold);
    }


    // Create a String of the form month/day/year
    public String toString ()
    {
	DecimalFormat twoDigits = new DecimalFormat ("00");
	DecimalFormat fourDigits = new DecimalFormat ("0000");
	return twoDigits.format (month) + "/" + twoDigits.format (day) + "/" + fourDigits.format (year);
    }
} // end class MyDate
