date.java

来自「《Java面向对象程序设计》例子源代码.轻松学习书本.」· Java 代码 · 共 37 行

JAVA
37
字号
//Date.java
public class Date {
   private int month;  
   private int day;    
   private int year;   
   public Date( int theMonth, int theDay, int theYear ){
      month = checkMonth( theMonth ); 
      year = theYear;                 
      day = checkDay( theDay );       
      System.out.println( "Date object constructor for date " + 
         toDateString() );
   } 
   private int checkMonth( int testMonth ){			//检测月份的合法性
      if ( testMonth > 0 && testMonth <= 12 )  
         return testMonth;
      else { 
         System.out.println( "Invalid month (" + testMonth + 
            ") set to 1." );
         return 1; 
      }
   } 
   private int checkDay( int testDay ){				//检测日期的合法性
      int daysPerMonth[] = 
         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
      if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
         return testDay;
      if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
           ( year % 4 == 0 && year % 100 != 0 ) ) )
         return testDay;  
      System.out.println( "Invalid day (" + testDay + ") set to 1." );
      return 1;  
   } 
   public String toDateString(){ 
      return month + "/" + day + "/" + year; 
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?