⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydate.java

📁 基础性的JAVA源代码
💻 JAVA
字号:
package exec.day0918;

import java.util.GregorianCalendar;
/**
 * 请定义MyDate类,该类的父类为 
 * GregorianCalendar(java.util包中)。类中只
 * 关心年月日三个属性。请提供有年月日三个参
 * 数的构造方法,并对构造方法中对参数进行校
 * 验。请提供获取年、月、日、星期的方法
 * @author Administrator
 *
 */
public class MyDate extends GregorianCalendar{
	public MyDate(int year,int month,int day){
		super(year,month-1,day);
		validateDate(year,month,day);
	}
	
	/**
	 * 对年月日的合法性进行校验
	 * @param year
	 * @param month
	 * @param day
	 */
	public void validateDate(int year,int month,int day){
		//对年份进行校验
		if(year<1900 || year>2100){
			throw new RuntimeException("错误的年份!");
		}
		
		//对月份进行校验
		if(month<1 || month>12){
			throw new RuntimeException("错误的月份!");
		}
		
		//对日期进行校验
		int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
		if(this.isLeapYear(year)){
			daysOfMonth[1] = 29;
		}
		if(day<1 || day>daysOfMonth[month-1]){
			throw new RuntimeException("不合法的日期!");
		}
	}
	
	public int getYear(){
		return this.get(YEAR);
	}
	public int getMonth(){
		return this.get(MONTH);
	}
	public int getDay(){
		return this.get(DAY_OF_MONTH);
	}
	
	public int getWeekDay(){
		return this.get(DAY_OF_WEEK);
	}
}












⌨️ 快捷键说明

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