All pastes #250611 Raw Edit

Something

public text v1 · immutable
#250611 ·published 2006-11-17 03:16 UTC
rendered paste body

//This class checks a date to see if it is an actual date in the common era.  It accounts for leap //years and the switch in 1752 from the Julian to the Gregorian calender.

public class MSDate {

	// Data fields
	int year;
	int month;
	int day;


	// Create a new date given input data for all data fields.
	public MSDate( int year, int month, int day ) {
		setYear( year );
		setMonth( month );
		setDay( day );
	}

	// declare constant (static final variable)
	public static final int JANUARY   =  1;
	public static final int FEBRUARY  =  2;
	public static final int MARCH     =  3;
	public static final int APRIL     =  4;
	public static final int MAY       =  5;
	public static final int JUNE      =  6;
	public static final int JULY      =  7;
	public static final int AUGUST    =  8;
	public static final int SEPTEMBER =  9;
	public static final int OCTOBER   = 10;
	public static final int NOVEMBER  = 11;
	public static final int DECEMBER  = 12;


	// Part of a method that tells you whether
	// a date is legal.  returns true or false.
	public boolean islegalDate() {
		switch( month ) {
			case SEPTEMBER:
				if ( islegalDay1752() ) {
					return true;
				}
				else {
					return false;
				}


			case APRIL:
			case JUNE:
			case NOVEMBER:
				if ( ( day > 0 ) && ( day <= 30 ) ) {
					return true;
				}
				else {
					return false;
				}


			case FEBRUARY:
				// February has 29 days
				if ( ( isLeapYear() ) && ( day > 0 ) && ( day <= 29 ) ) {
					return true;
				}

				else if ( ( day > 0 ) && ( day <= 28 ) ) {
					return true;

				}
				break;

			default:
				if ( ( day > 0 ) && ( day <= 31 ) ) {
					return true;
				}
				else {
					return false;
				}

		}

	}

    public boolean islegalDay1752() {
        if ( (year == 1752) && (day >= 3 || day <= 3) ) {
            return false;
		}
        else if (year == 1752) {
            return true;
        }
	}

    public boolean isLeapYear() {
        if ( (year >= 1) && (year <= 1752) && (year % 4 == 0) ) {
            return true;
        }
        else if ( (year > 1752) && (year % 4 == 0) && (year % 400 == 0)) {
            return true;
        }
	}

	// "get" methods for getting each data field
	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}

	// "set" methods for setting the value of each datafield.
	public void setYear( int y ) {
		year = y;
	}

	public void setMonth( int m ) {
		month = m;
	}

	public void setDay( int d ) {
		day = d;
	}

	// Print a date.
	public String toString() {
		return( year + " - " + month + " - " + day );
	}

}