Source of Date.java


  1: // Fig. 8.7: Date.java 
  2: // Date class declaration.
  3: 
  4: public class Date 
  5: {
  6:    private int month; // 1-12
  7:    private int day;   // 1-31 based on month
  8:    private int year;  // any year
  9: 
 10:    // constructor: call checkMonth to confirm proper value for month; 
 11:    // call checkDay to confirm proper value for day
 12:    public Date( int theMonth, int theDay, int theYear )
 13:    {
 14:       month = checkMonth( theMonth ); // validate month
 15:       year = theYear; // could validate year
 16:       day = checkDay( theDay ); // validate day
 17: 
 18:       System.out.printf( 
 19:          "Date object constructor for date %s\n", this );
 20:    } // end Date constructor
 21: 
 22:    // utility method to confirm proper month value
 23:    private int checkMonth( int testMonth )
 24:    {
 25:       if ( testMonth > 0 && testMonth <= 12 ) // validate month
 26:          return testMonth;
 27:       else // month is invalid 
 28:       { 
 29:          System.out.printf( 
 30:             "Invalid month (%d) set to 1.", testMonth );
 31:          return 1; // maintain object in consistent state
 32:       } // end else
 33:    } // end method checkMonth
 34: 
 35:    // utility method to confirm proper day value based on month and year
 36:    private int checkDay( int testDay )
 37:    {
 38:       int daysPerMonth[] = 
 39:          { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 40:    
 41:       // check if day in range for month
 42:       if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
 43:          return testDay;
 44:    
 45:       // check for leap year
 46:       if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
 47:            ( year % 4 == 0 && year % 100 != 0 ) ) )
 48:          return testDay;
 49:    
 50:       System.out.printf( "Invalid day (%d) set to 1.", testDay );
 51:       return 1;  // maintain object in consistent state
 52:    } // end method checkDay
 53:    
 54:    // return a String of the form month/day/year
 55:    public String toString()
 56:    { 
 57:       return String.format( "%d/%d/%d", month, day, year ); 
 58:    } // end method toString
 59: } // end class Date
 60: 
 61: 
 62: /**************************************************************************
 63:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 64:  * Pearson Education, Inc. All Rights Reserved.                           *
 65:  *                                                                        *
 66:  * DISCLAIMER: The authors and publisher of this book have used their     *
 67:  * best efforts in preparing the book. These efforts include the          *
 68:  * development, research, and testing of the theories and programs        *
 69:  * to determine their effectiveness. The authors and publisher make       *
 70:  * no warranty of any kind, expressed or implied, with regard to these    *
 71:  * programs or to the documentation contained in these books. The authors *
 72:  * and publisher shall not be liable in any event for incidental or       *
 73:  * consequential damages in connection with, or arising out of, the       *
 74:  * furnishing, performance, or use of these programs.                     *
 75:  *************************************************************************/