Class—日期类设计(第二版)

package atest;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;
        
        int choice = input.nextInt();
 
        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
 
            DateUtil date = new DateUtil(year, month, day);
 
            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();
 
            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
 
            DateUtil date = new DateUtil(year, month, day);
 
            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
 
            n = input.nextInt();
 
            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
 
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());
 
            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());
 
            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 
            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println( fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class Day {
	private int value = 0;
	private Month month;
	private int[] month_maxnum = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
	private int day_max=0;
	
	// 默认构造方法
	public Day() {
	}
	
	// 带参构造方法
	public Day(int yearValue, int monthValue, int dayValue) {
		 setValue(dayValue);
		 Month month = new Month(yearValue,monthValue);
		 setMonth(month);
	}
	
	// value getter 
	public int getValue() {
		return value;
	}
	
	// value setter
	public void setValue(int vlaue) {
		this.value = vlaue;
	}
	
	// month getter
	public Month getMonth() {
		return month;
	}
	
	// month setter
	public void setMonth(Month month) {
		this.month = month;
	}
	
	// 日期复位 月份增加
	public void restMin() {
		value = 1;
		month.monthIncrement();
		if(month.getValue() == 13) {
			month.restMin();
		}
	}
	
	// 日期设为改月最大值
	public int restMax() {
		if(month.leapYearMonthTwo())
			month_maxnum[2]=29;
		day_max = month_maxnum[month.getValue()];
		return day_max;
	}
	
	// 这个方法是自己加的,用于更改2月份天数
	public void changeMonthTwo() {
		if(month.leapyearJudge())
			month_maxnum[2]=29;
		else
			month_maxnum[2]=28;
	}
	
	// 检验数据合法性
	public boolean validate() {
		if(month.leapYearMonthTwo())
			month_maxnum[2]=29;
		if(month.validate() && value >= 1 && value <=month_maxnum[month.getValue()])
			return true;
		else
			return false;
	}
	
	// 日期增一
	public void dayIncrement() {
		value++;
	}
	
	//日期减一
	public void dayReduction() {
		value--;
	}
}

class Month {
	private int value = 0;
	private Year year;
	
	// 默认构造方法
	public Month() {
	}
	
	// 带参构造方法
	public Month(int yearValue, int monthValue) {
		value = monthValue;
		Year year = new Year(yearValue);
		setYear(year);
	}
	
	// value getter
	public int getValue() {
		return value;
	}
	
	// value setter
	public void setValue(int value) {
		this.value = value;
	}

	// year getter
	public Year getYear() {
		return year;
	}

	// year setter
	public void setYear(Year year) {
		this.year = year;
	}
	
	// 月份复位 同时年份加一
	public void restMin() {
		value = 1;
		year.yearIncrement();
	}
	
	// 月份设置为12
	public void restMax() {
		
	}
	
	// 传递闰年的信息
	public boolean leapyearJudge() {
		if(year.isLeapYear())
			return true;
		else 
			return false;
	}
	
	// 这个方法自己加的,是用于辅助更改2月份闰年的天数
	public boolean leapYearMonthTwo() {
		if(year.isLeapYear() && value == 2)
			return true;
		else
			return false;
	}
	
	// 检验数据合法性
	public boolean validate() {
		if(year.validate() && value >= 1 && value <= 12 )
			return true;
		else
			return false;
	}
	
	// 月份增一
	public void monthIncrement() {
		value++;	
	}
	
	// 月份减一
	public void monthReduction() {
		value--;
	}
}

class Year {
	private int value = 0;

	// 无参构造方法
	public Year() {	
	}

	// 有参构造方法
	public Year(int value) {
		this.value = value;
	}

	// value getter
	public int getValue() {
		return value;
	}

	// value setter
	public void setValue(int value) {
		this.value = value;
	}
	
	// 判断是否是闰年
	public boolean isLeapYear() {
		if((value % 4 == 0 &&  value % 100 != 0)||(value % 400 == 0))
            return true;
        else
            return false;
	}
	
	// 校验数据的合法性
	public boolean validate() {
		if(value >= 1900 && value <= 2050 )
			return true;
		else
			return false;
	}
	
	// 年份增一
	public void yearIncrement() {
		value++;
	}
	
	// 年份减一
	public void yearReduction() {
		value--;
	}
}

class DateUtil {
	private Day day;
	
	// 无参构造方法
	DateUtil(){
	}
	
	// 有参构造方法
	DateUtil(int d, int m, int y){
		Day day = new Day(d,m,y);
		setDay(day);
	}

	// day getter
	public Day getDay() {
		return day;
	}

	// day setter
	public void setDay(Day day) {
		this.day = day;
	}
	
	// 检验数据合法性
	public boolean checkInputValidity() {
		if(day.validate())
			return true;
		else
			return false;
	}
	
	// 比较两个日期的大小
	public boolean compareDates(DateUtil date) {
		if(day.getMonth().getYear().getValue() < date.day.getMonth().getYear().getValue())
            return true;
        if(day.getMonth().getYear().getValue() > date.day.getMonth().getYear().getValue())
            return false;
        if(day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && day.getMonth().getValue() < date.day.getMonth().getValue())
            return true;
        if(day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && day.getMonth().getValue() > date.day.getMonth().getValue())
            return false;
        if(day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && day.getMonth().getValue() == date.day.getMonth().getValue() &&day.getValue() < date.day.getValue())
            return true;
        if(day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && day.getMonth().getValue() == date.day.getMonth().getValue() &&day.getValue() > date.day.getValue())
            return false;
        return true;
	}
	
	// 判定两个日期是否相等
	public boolean equalTwoDatesDateUtil(DateUtil date) {
		if(day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() &&
				day.getMonth().getValue() == date.day.getMonth().getValue() &&
				day.getValue() == date.day.getValue())
			return true;
		else
			return false;
	}
	
	// 日期值格式化
	public String showDate() {
		return "" + day.getMonth().getYear().getValue() + '-' + day.getMonth().getValue() + '-' + day.getValue();
	}
	
	// 求下n天
	public DateUtil getNextNDays(int n) {
		while(n > 0) {
			day.dayIncrement();
			if(day.getValue() > day.restMax()) {
				day.restMin();
				day.changeMonthTwo();
			}
			n--;
		}
		return this;
	}
	
	// 求前n天
	public DateUtil getPreviousNDays(int n) {
		while(n > 0) {
			day.dayReduction();
			if(day.getValue() == 0) {
				if(day.getMonth().getValue()==1) {
					day.getMonth().setValue(12);
					day.setValue(31);
					day.getMonth().getYear().yearReduction();
					day.changeMonthTwo();
				}
				else {
					day.getMonth().monthReduction();
					day.setValue(day.restMax());
				}
			}
			n--;
		}
		return this;
	}
	
	//得到日期之间的天数
	public int getDaysofDates(DateUtil date) {
		int cnt = 0;
		if(compareDates(date) != true) {
			int a,b;
			a=day.getMonth().getYear().getValue();
			b=date.day.getMonth().getYear().getValue();
			date.day.getMonth().getYear().setValue(a);
			day.getMonth().getYear().setValue(b);
			a=day.getMonth().getValue();
			b=date.day.getMonth().getValue();
			day.getMonth().setValue(b);
			date.day.getMonth().setValue(a);
			a=day.getValue();
			b=date.day.getValue();
			day.setValue(b);
			date.day.setValue(a);
		}
        if(equalTwoDatesDateUtil(date)) {
            cnt = 0;
        }
        else {
        	while(day.getMonth().getYear().getValue() <= (date.day.getMonth().getYear().getValue()-2)) {
        		if(( (day.getMonth().getYear().getValue()+1)%4==0&&(day.getMonth().getYear().getValue()+1)%100!=0) ||(day.getMonth().getYear().getValue()+1)%400 ==0) {
        			day.getMonth().getYear().yearIncrement();
        			cnt= cnt + 366;
        		}
        		else {
        			day.getMonth().getYear().yearIncrement();
        			cnt= cnt + 365;
        		}
        	}
            while(equalTwoDatesDateUtil(date) != true) {
            day.dayIncrement();            
            cnt++;
            if(day.getValue() > day.restMax()) {
                day.restMin();
                day.changeMonthTwo();
                }
            }
        }
        return cnt;
	}
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值