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


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.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:"); 
            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.print(
                    date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
            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("The days between " + fromDate.showDate() + 
                        " and " + toDate.showDate() + " are:"
                        + 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;
	
	// 无参构造方法
	Day() {	
	}
	
	// 有参构造方法
	Day(int value) {
	}
	
	// value getter
	public int getValue() {
		return value;
	}
	
	// value setter
	public void setValue(int value) {
		this.value = value;
	}
	
	// 日期加一
	public void dayIncrement() {
		value++;
	}
	
	// 日期减一 
	public void dayReduction() {
		value--;
	}
}

class Month {
	private int value = 0;
	
	// 无参构造方法
	Month() {	
	}
	
	// 有参构造方法
	Month(int value) {
		
	}
	
	// value getter
	public int getValue() {
		return value;
	}
	
	// value setter
	public void setValue(int value) {
		this.value = value;
	}
	
	// 月份复位(1)
	public void resetMin() {
		value =1;
	}
	
	// 月份设置最大值(12)
	public void resetMax() {
		value =12;
	}
	
	// 数据合法性校验
	public boolean validate() {
		if(value >= 1 && value <= 12)
			return true;
		else
			return false;
	}
	
	// 月加一
	public void monthIncrement() {
		value++;
	}
	
	// 月减一
	public void monthReduction() {
		value--;
	}
}

class Year {
	private int value = 0;
	
	// 无参构造方法
	Year() {
	}
	
	// 有参构造方法
	Year(int 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 >= 1820 && value <= 2020)
			return true;
		else
			return false;
	}
	
	// 年加一
	public void yearIncrement() {
		value++;
	}
	
	// 年减一
	public void yearReduction() {
		value--;
	}
}

class DateUtil {
	private Day day = new Day();
	private Month month = new Month();
	private Year year = new Year();
	private int[] mon_maxnum = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
	
	// 无参构造方法
	DateUtil() {
	}
	
	// 有参构造方法
	DateUtil(int y,int m,int d){
		day.setValue(d);
		month.setValue(m);
		year.setValue(y);
	}

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

	// Day setter
	public void setDay(Day day) {
		this.day = day;
	}

	// Month getter
	public Month getMonth() {
		return month;
	}

	// Month setter
	public void setMonth(Month month) {
		this.month = month;
	}

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

	// Year setter
	public void setYear(Year year) {
		this.year = year;
	}
	
	// 设置日期最小值
	public void setDayMin() {
		day.setValue(1);
	}
	
	// 设置日期——当月最大天数
	public void setDayMax() {
		if(year.isLeapYear())
			mon_maxnum[2] = 29;
		else
			mon_maxnum[2] = 28;
	}
	
	// 检验输入数据合法性
	public boolean checkInputValidity() {
		if(year.isLeapYear())
			mon_maxnum[2] = 29;
		else
			mon_maxnum[2] = 28;
		if(month.validate() && year.validate() && day.getValue()>=1 && day.getValue() <= mon_maxnum[month.getValue()])
			return true;
		else
			return false;
	}
	
	// 求下n天
	public DateUtil getNextNDays(int n) {
		while(n > 0) {
			day.dayIncrement();
			if(day.getValue() > mon_maxnum[month.getValue()]) {
				setDayMin();
				month.monthIncrement();
				if(month.getValue() == 13) {
					month.resetMin();
					year.yearIncrement();
					if(year.isLeapYear())
						mon_maxnum[2] = 29;
					else
						mon_maxnum[2] = 28;
				}
			}
			n--;
		}
		return this;
	}
	
	// 求前n天
	public DateUtil getPreviousNDays(int n) {
		while(n > 0) {
			day.dayReduction();
			if(day.getValue() == 0) {
				if(month.getValue() == 1) {
					month.resetMax();
					day.setValue(31);
					year.yearReduction();
					if(year.isLeapYear())
						mon_maxnum[2] = 29;
					else
						mon_maxnum[2] = 28;
				}
				else {
					month.monthReduction();
					day.setValue(mon_maxnum[month.getValue()]);
				}
			}
			n--;
		}
		return this;
	}
	
	// 判断两个日期的先后
	public boolean compareDates(DateUtil date) {
		if(year.getValue() < date.year.getValue())
            return true;
        if(year.getValue() > date.year.getValue())
            return false;
        if(year.getValue() == date.year.getValue() && month.getValue() < date.month.getValue())
            return true;
        if(year.getValue() == date.year.getValue() && month.getValue() > date.month.getValue())
            return false;
        if(year.getValue() == date.year.getValue() && month.getValue() == date.month.getValue() &&day.getValue() < date.day.getValue())
            return true;
        if(year.getValue() == date.year.getValue() && month.getValue() == date.month.getValue() &&day.getValue() > date.day.getValue())
            return false;
        return true;
	}
	
	// 判断两个日期是否相等
	public boolean equalTwoDates(DateUtil date) {
		if(year.getValue() == date.year.getValue() && month.getValue() == date.month.getValue() && day.getValue() == date.day.getValue())
            return true;
        else
            return false;
	}
	
	// 求两个日期的天数差
	public int getDaysofDates(DateUtil date) {
		int cnt = 0;
		if(compareDates(date) != true) {
			int a,b;
			a=day.getValue();
			b=date.day.getValue();
			date.day.setValue(a);
			day.setValue(b);
			
			a=month.getValue();
			b=date.month.getValue();
			month.setValue(b);
			date.month.setValue(a);
			
			a=year.getValue();
			b=date.year.getValue();
			year.setValue(b);
			date.year.setValue(a);
		}
        if(equalTwoDates(date) == true) {
            cnt = 0;
        }
        else {
            while(equalTwoDates(date) != true) {
            day.dayIncrement();            
            cnt++;
            if(day.getValue() > mon_maxnum[month.getValue()]) {
                setDayMin();
                month.monthIncrement();
                if(month.getValue() == 13) {
                    month.resetMin();;
                    year.yearIncrement();
                    if(year.isLeapYear())
						mon_maxnum[2] = 29;
					else
						mon_maxnum[2] = 28;
                }
            }
        }
        }
        return cnt;
        
	}
	
	// 日期格式化输出
	public String showDate() {
		return "" + year.getValue() + '-' + month.getValue() + '-' + day.getValue();
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值