日期问题面向对象设计(聚合二)

参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

在这里插入图片描述
应用程序共测试三个功能:

求下n天
求前n天
求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):

1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
当输入有误时,输出格式如下: Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2
当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2
当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值
输入样例1:
在这里给出一组输入。例如:

3 2014 2 14 2020 6 14
输出样例1:
在这里给出相应的输出。例如:

The days between 2014-2-14 and 2020-6-14 are:2312
输入样例2:
在这里给出一组输入。例如:

2 1834 2 17 7821
输出样例2:
在这里给出相应的输出。例如:

1834-2-17 previous 7821 days is:1812-9-19
输入样例3:
在这里给出一组输入。例如:

1 1999 3 28 6543
输出样例3:
在这里给出相应的输出。例如:

1999-3-28 next 6543 days is:2017-2-24
输入样例4:
在这里给出一组输入。例如:

0 2000 5 12 30
输出样例4:
在这里给出相应的输出。例如:

Wrong Format

import java.util.Scanner;

class Day {
	private int yearValue;
    private int monthValue;
    private int dayValue;
	
	public Day() { //默认构造方法
		
	}
	
	public Day(int yearValue,int monthValue,int dayValue) {
		this.yearValue = yearValue;
		this.monthValue = monthValue;
		this.dayValue = dayValue;
	}
	
	public void setYear(int yearValue) {
        this.yearValue = yearValue;
    }

    public void setMonth(int monthValue) {
        this.monthValue = monthValue;
    }

    public void setDay(int dayValue) {
        this.dayValue = dayValue;
    }

    public int getYear() {
        return yearValue;
    }

    public int getMonth() {
        return monthValue;
    }

    public int getDay() {
        return dayValue;
    }
}

class Month {
	private int yearValue;
    private int monthValue;
    private int dayValue;
	
	public Month() { //默认构造方法
		
	}
	
	public Month(int yearValue,int monthValue,int dayValue) {
		this.yearValue = yearValue;
		this.monthValue = monthValue;
		this.dayValue = dayValue;
	}
	
	public void setYear(int yearValue) {
        this.yearValue = yearValue;
    }

    public void setMonth(int monthValue) {
        this.monthValue = monthValue;
    }

    public void setDay(int dayValue) {
        this.dayValue = dayValue;
    }

    public int getYear() {
        return yearValue;
    }

    public int getMonth() {
        return monthValue;
    }

    public int getDay() {
        return dayValue;
    }
	
}

class Year {
	private int yearValue;
    private int monthValue;
    private int dayValue;
	
	public Year() { //默认构造方法
		
	}
	
	public Year(int yearValue,int monthValue,int dayValue) {
		this.yearValue = yearValue;
		this.monthValue = monthValue;
		this.dayValue = dayValue;
	}
	
	public void setYear(int yearValue) {
        this.yearValue = yearValue;
    }

    public void setMonth(int monthValue) {
        this.monthValue = monthValue;
    }

    public void setDay(int dayValue) {
        this.dayValue = dayValue;
    }

    public int getYear() {
        return yearValue;
    }

    public int getMonth() {
        return monthValue;
    }

    public int getDay() {
        return dayValue;
    }
	
}

class DateUtil {
    private long year;
    private long month;
    private long day;

    public DateUtil() { //默认构造方法
    	
    }
    
    public DateUtil(long year, long month, long day) { //带参构造方法
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    public void setYear(long year) {
        this.year = year;
    }

    public void setMonth(long month) {
        this.month = month;
    }

    public void setDay(long day) {
        this.day = day;
    }

    public long getYear() {
        return year;
    }

    public long getMonth() {
        return month;
    }

    public long getDay() {
        return day;
    }
    
    private final long[] mon_maxnum=new long[]{31,28,31,30,31,30,31,31,30,31,30,31};
    
    public boolean isLeapYear(long year) { //判断year是否为闰年
//        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
//     	   return true;
//        }
//        else {
//     	   return false;
//        }
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;//就是这个地方,导致内存超限,可能是因为直接return的效率更高
    }

   private long getDayOfMonth(long year, long month) {
//	   long[] mon_maxnum=new long[]{31,28,31,30,31,30,31,31,30,31,30,31};
        long days = mon_maxnum[(int) (month - 1)];
        if (month == 2 && isLeapYear(year)) {
            days = 29;
        } else {
        	
        }
        return days;
    }
    
    public  boolean checkInputValidity() { //检查日期是否合法
        int[] mon_maxnum=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year) == true) {
            mon_maxnum[1]=29;
        }
        else {
            mon_maxnum[1]=28;
        }
        if(year < 1820 || year > 2020 || month < 1 || month > 12 || mon_maxnum[(int) (month - 1)] < day || day < 1) {
            return false;
        }
//        if(isLeapYear(year) == true && month == 2 && day > 29 ) {
//    		return false;
//    	}
        else {
            return true;
        }
    }

    public DateUtil getNextNDays(long n) { //下n天
        long year = this.year;
        long month = this.month;
        long day = this.day;
        
        for (int i = 0; i < n; i++) {
            day++;
            if (day > getDayOfMonth(year, month)) {
                day = 1;
                month++;
                if (month > 12) {
                    month = 1;
                    year++;
                } else {
                	
                }
             } else {
            	
            }
        }
        return new DateUtil(year, month, day);
    }

    public DateUtil getPreviousNDays(long n) { //前n天
        long year = this.year;
        long month = this.month;
        long day = this.day;
        for (int i = 0; i < n; i++) {
            day--;
            while (day < 1) {
                month--;
                if (month < 1) {
                    month = 12;
                    year--;
                } else {
                	
                }
                day += getDayOfMonth(year, month);
            }
        }
        return new DateUtil(year, month, day);
    }

    public boolean compareDates(DateUtil date) { //比较当前日期与date的大小
        if (this.year > date.year) {
        	return true;
        } else {
        	if (this.year == date.year) {
        		if (this.month > date.month) {
                	return true;
                } else {
                	if (this.month == date.month) {
                        if (this.day >= date.day) {
                        	return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
    {
        if (date != null) {
            if (year == date.year && month == date.month && day == date.day) {
                return true;
            } else {
            	
            }
        } else {
        	
        }
        return false;
    }
    
    private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    
    public long getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
    {
        DateUtil dateUtil1 = this; // 小
        DateUtil dateUtil2 = date; // 大
        if (this.compareDates(date)) {
            dateUtil1 = date;
            dateUtil2 = this;
        } else {
        	
        }

        long days;
        int leapYearNum = 0;
        for (long i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
            if (isLeapYear(i)) {
                leapYearNum++;
            } else {
            	
            }
        }

        days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;

        long d1 = mon[(int) (dateUtil1.getMonth() - 1)] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
        long d2 = mon[(int) (dateUtil2.getMonth() - 1)] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
        return days - d1 + d2;
    }
    
    public String showDate()//以“year-month-day”格式返回日期值
    {
    	String str=year + "-" + month + "-" + day;
        return str;
    }
    
//     public boolean checkDate() { //判断日期是否正确
//         if()
//     }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long year;
        long month;
        long day;
        long year2;
        long month2;
        long day2;
        
        int number;
        number = in.nextInt();
        
        if(number == 0) {
        	System.out.println("Wrong Format");
        	System.exit(0);
        }
        
        if (number == 1) { //下n天
            long m;
            year = in.nextLong();
            month = in.nextLong();
            day = in.nextLong();
            
            DateUtil date = new DateUtil(year, month, day);

            if (date.checkInputValidity() == false) { //检查日期是否合法
                System.out.println("Wrong Format");
                System.exit(0);
            } else {
            	
            }

            m = in.nextLong();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            } else {
                
            }
            
            if(date.getNextNDays(m).checkInputValidity() == false) {  //日期越界情况
//             	System.out.println("Wrong Format");
//                 System.exit(0);
            } else {
            	
            }
            
            //year1-month1-day1 next n days is:year2-month2-day2
//            System.out.print(year+"-"+month+"-"+day+" next "+m+" days is:");
            System.out.print(date.showDate()+" next "+m+" days is:");
            System.out.println(date.getNextNDays(m).showDate());
            date = null;
            System.exit(0);
        } else if (number == 2) { //前n天
            long n = 0;
            year = in.nextLong();
            month = in.nextLong();
            day = in.nextLong();

            DateUtil date = new DateUtil(year, month, day);
           
            if (date.checkInputValidity() == false) { //检查日期是否合法
                System.out.println("Wrong Format");
                System.exit(0);
            } else {
            	
            }

            n = in.nextLong();

            if (n < 0) {
            	System.out.println("Wrong Format");
                System.exit(0);
            } else {
            	
            }
            
            if(date.getPreviousNDays(n).checkInputValidity() == false) {  //日期越界情况
//             	System.out.println("Wrong Format");
//                 System.exit(0);
            } else {
            	
            }
            
            //year1-month1-day1 previous n days is:year2-month2-day2
            System.out.print(date.showDate()+" previous "+n+" days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
            date = null;
            System.exit(0);
        } else if (number == 3) { //两天之间相差的天数
            year = in.nextLong();
            month = in.nextLong();
            day = in.nextLong();

            year2 = in.nextLong();
            month2 = in.nextLong();
            day2 = in.nextLong();

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(year2, month2, day2);

            if (fromDate.checkInputValidity() == true && toDate.checkInputValidity() == true) { //检查日期的合法性
                
                //The days between year1-month1-day1 and year2-month2-day2 are:值
                System.out.println("The days between "+year+"-"+month+"-"+day+" and "+year2+"-"+month2+"-"+day2+" are:"+fromDate.getDaysofDates(toDate));
                System.exit(0);
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P-chanY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值