Java 日期类设计:DateUtil 类实现
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);
}
}
}
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 % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
public DateUtil getNextNDays(int n) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int newYear = year;
int newMonth = month;
int newDay = day + n;
if (newDay > daysInMonth[month - 1]) {
if (month == 2 && isLeapYear(year)) {
if (newDay > 29) {
newDay -= 29;
newMonth++;
}
} else {
newDay -= daysInMonth[month - 1];
newMonth++;
}
if (newMonth > 12) {
newMonth -= 12;
newYear++;
}
}
return new DateUtil(newYear, newMonth, newDay);
}
public DateUtil getPreviousNDays(int n) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int newYear = year;
int newMonth = month;
int newDay = day - n;
if (newDay < 1) {
if (month == 3 && isLeapYear(year)) {
newDay += 29;
newMonth--;
} else {
newDay += daysInMonth[month - 2];
newMonth--;
}
if (newMonth < 1) {
newMonth += 12;
newYear--;
}
}
return new DateUtil(newYear, newMonth, newDay);
}
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 count = 0;
DateUtil tempDate = new DateUtil(year, month, day);
while (!tempDate.equalTwoDates(date)) {
tempDate = tempDate.getNextNDays(1);
count++;
}
return count;
}
public String showDate() {
return year + '-' + month + '-' + day;
}
}
原文地址: https://www.cveoy.top/t/topic/o7cp 著作权归作者所有。请勿转载和采集!