7-2:日期类设计:

设计一个名为DateUtil的类,包含年、月、日属性并实现日期合法性检查、闰年判断、日期增减、日期比较等功能。通过主类测试不同场景下的日期操作,如求下n天、前n天及日期间隔天数。要求不使用Java内置日期类。
摘要由CSDN通过智能技术生成

参考题目集二中和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:
public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值

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

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() + "-" + date.getMonth() + "-" + date.getDay() + " 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() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
			System.out.println(date.getPreviousNDays(n).showDate());
		} else if (choice == 3) {
   	//test getDaysofDates method
			year = Integer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值