Java 日期工具类 DateUtil - 无需使用内置日期类的日期计算
Java 日期工具类 DateUtil - 无需使用内置日期类的日期计算
本代码示例实现了一个名为 DateUtil 的 Java 类,用于进行日期计算,包括获取日期的下一个日期、上一个日期、计算两个日期之间的天数等等。该类在实现过程中避免了使用 Java 内置的日期类和方法,仅使用基础的数学运算实现。
类定义
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;
}
// Getter 和 Setter 方法
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;
}
// 判断 year 是否为闰年
public boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
return false;
}
// 取得 year-month-day 的下 n 天日期
public DateUtil getNextNDays(int n) {
int daysInMonth = getDaysInMonth(year, month);
int nextDay = day + n;
int nextMonth = month;
int nextYear = year;
while (nextDay > daysInMonth) {
nextDay -= daysInMonth;
nextMonth++;
if (nextMonth > 12) {
nextMonth = 1;
nextYear++;
}
daysInMonth = getDaysInMonth(nextYear, nextMonth);
}
return new DateUtil(nextYear, nextMonth, nextDay);
}
// 取得 year-month-day 的前 n 天日期
public DateUtil getPreviousNDays(int n) {
int prevDay = day - n;
int prevMonth = month;
int prevYear = year;
while (prevDay < 1) {
prevMonth--;
if (prevMonth < 1) {
prevMonth = 12;
prevYear--;
}
int daysInPrevMonth = getDaysInMonth(prevYear, prevMonth);
prevDay += daysInPrevMonth;
}
return new DateUtil(prevYear, prevMonth, prevDay);
}
// 比较当前日期与 date 的大小(先后)
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;
}
// 求当前日期与 date 之间相差的天数
public int getDaysofDates(DateUtil date) {
int days = 0;
DateUtil currentDate = new DateUtil(year, month, day);
while (!currentDate.equalTwoDates(date)) {
currentDate = currentDate.getNextNDays(1);
days++;
}
return days;
}
// 以“year-month-day”格式返回日期值
public String showDate() {
return year + "- " + month + "- " + day;
}
// 私有方法,用于获取指定年份和月份的天数
private int getDaysInMonth(int year, int month) {
int daysInMonth;
if (month == 2) {
if (isLeapYear(year)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
return daysInMonth;
}
}
测试代码
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]):
year month day n// 测试输入日期的下 n 天year month day n// 测试输入日期的前 n 天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:值
注意
请注意,此实现假设所有的输入都是合法的。如果输入不合法,需要在程序中进行额外的错误处理。
原文地址: https://www.cveoy.top/t/topic/o9Fi 著作权归作者所有。请勿转载和采集!