📄 date.java
字号:
//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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -