Java DateUtil 类实现 - 计算日期之间的天数、获取前后 n 天日期
Java DateUtil 类实现 - 计算日期之间的天数、获取前后 n 天日期
本程序使用自定义 DateUtil 类实现日期操作,无需使用 Java 内置日期类,可以计算两个日期之间相差的天数、获取指定日期的前后 n 天日期等功能。
程序要求:
- 设计一个名为 DateUtil 的类,包含三个私有属性 year、month、day(均为整型数),其中 year ∈ [1820, 2020],month ∈ [1, 12],day ∈ [1, 31]。
- 除了创建该类的构造方法、属性的 getter 及 setter 方法外,还需要编写以下方法:
checkInputValidity(): 检测输入的年、月、日是否合法。isLeapYear(int year): 判断 year 是否为闰年。getNextNDays(int n): 取得 year-month-day 的下 n 天日期。getPreviousNDays(int n): 取得 year-month-day 的前 n 天日期。compareDates(DateUtil date): 比较当前日期与 date 的大小(先后)。equalTwoDates(DateUtil date): 判断两个日期是否相等。getDaysofDates(DateUtil date): 求当前日期与 date 之间相差的天数。showDate(): 以 'year-month-day' 格式返回日期值。
程序主方法:
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.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);
}
}
}
输入格式:
有三种输入方式(以输入的第一个数字划分[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:值
DateUtil 类代码:
public class DateUtil {
private int year;
private int month;
private int day;
public DateUtil(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public boolean checkInputValidity() {
if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31) {
return false;
}
return true;
}
public boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
public DateUtil getNextNDays(int n) {
int tempDay = day + n;
int tempYear = year;
int tempMonth = month;
while (tempDay > getDaysOfMonth(tempYear, tempMonth)) {
tempDay -= getDaysOfMonth(tempYear, tempMonth);
tempMonth++;
if (tempMonth > 12) {
tempMonth = 1;
tempYear++;
}
}
return new DateUtil(tempYear, tempMonth, tempDay);
}
public DateUtil getPreviousNDays(int n) {
int tempDay = day - n;
int tempYear = year;
int tempMonth = month;
while (tempDay < 1) {
tempMonth--;
if (tempMonth < 1) {
tempMonth = 12;
tempYear--;
}
tempDay += getDaysOfMonth(tempYear, tempMonth);
}
return new DateUtil(tempYear, tempMonth, tempDay);
}
public boolean compareDates(DateUtil date) {
if (year < date.getYear()) {
return true;
} else if (year > date.getYear()) {
return false;
} else {
if (month < date.getMonth()) {
return true;
} else if (month > date.getMonth()) {
return false;
} else {
if (day < date.getDay()) {
return true;
} else {
return false;
}
}
}
}
public boolean equalTwoDates(DateUtil date) {
if (year == date.getYear() && month == date.getMonth() && day == date.getDay()) {
return true;
}
return false;
}
public int getDaysofDates(DateUtil date) {
int days = 0;
if (compareDates(date)) {
DateUtil temp = new DateUtil(year, month, day);
while (!temp.equalTwoDates(date)) {
temp = temp.getNextNDays(1);
days++;
}
} else {
DateUtil temp = new DateUtil(date.getYear(), date.getMonth(), date.getDay());
while (!temp.equalTwoDates(this)) {
temp = temp.getNextNDays(1);
days++;
}
}
return days;
}
public String showDate() {
return year + '-' + month + '-' + day;
}
private int getDaysOfMonth(int year, int month) {
int days = 0;
if (month == 2) {
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
return days;
}
}
通过上述代码,我们实现了 DateUtil 类,其中包含了题目要求的所有方法。你可以将上述代码复制粘贴到你的 IDE 中,然后运行 Main 类的 main 方法来进行测试。根据题目要求,你需要根据输入的数字来选择要测试的功能(1代表求下 n 天,2代表求前 n 天,3代表求两个日期相差的天数)。根据不同的选择,你需要提供相应的输入来测试对应的功能。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/o9Fp 著作权归作者所有。请勿转载和采集!