-->
当前位置:首页 > 题库

PROGRAMMING:Date design

Luz5年前 (2021-05-10)题库445
Referring to the date related programs in topic set 2, this paper designs a class dateutil, which has three private properties year, month and day (all integer numbers). Among them, year ∈ [18202020], month ∈ [1,12] and day ∈ [1,31]. In addition to the construction method of creating this class, the getter and setter methods of properties, the following methods need to be written:
```
public boolean checkInputValidity();// Check whether the input year, month and day are legal
public boolean isLeapYear(int year);// Judge whether year is a leap year
public DateUtil getNextNDays(int n);// Gets the date of the next n days of year month day
public DateUtil getPreviousNDays(int n);// Get the date n days before year month day
public boolean compareDates(DateUtil date);// Compare the size of the current date and date (in sequence)
public boolean equalTwoDates(DateUtil date);// Judge whether two dates are equal
public int getDaysofDates(DateUtil date);// Find the number of days difference between the current date and date
public String showDate();// Returns the date value in "Year Month Day" format
```
The application tests three functions
1. Find n days
1. Ask for the previous n days
1. Find the number of days between two dates
**Note: it is strictly forbidden to use any date related classes and methods provided in Java, and submit complete source code, including main classes and methods (provided, no need to modify)**
The main method of the program is as follows:
```
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);
}
}
}

```

### Input format:
There are three input methods (divided by the first number of input [1,3]):
*1 year month day n / / N days after the test input date
*2 year month day n / / N days before the test input date
*3 year1 month1 Day1 year2 month2 Day2 / / test the number of days between two dates
###Output format:
*When the input is wrong, the output format is as follows:
```Wrong Format```
*When the first digit is 1 and all inputs are valid, the output format is as follows:
```
year1-month1-day1 next n days is:year2-month2-day2
```
*When the first digit is 2 and all inputs are valid, the output format is as follows:
```
year1-month1-day1 previous n days is:year2-month2-day2
```
*When the first digit is 3 and all inputs are valid, the output format is as follows:
```
The days between year1-month1-day1 and year2-month2-day2 are: Values
```
###Input sample 1:
Here is a set of inputs. For example:
```in
3 2014 2 14 2020 6 14
```
###Output sample 1:
The corresponding output is given here. For example:
```out
The days between 2014-2-14 and 2020-6-14 are:2312
```
###Input sample 2:
Here is a set of inputs. For example:
```in
2 1834 2 17 7821
```
###Output sample 2:
The corresponding output is given here. For example:
```out
1834-2-17 previous 7821 days is:1812-9-19
```
###Input sample 3:
Here is a set of inputs. For example:
```in
1 1999 3 28 6543
```
###Output sample 3:
The corresponding output is given here. For example:
```out
1999-3-28 next 6543 days is:2017-2-24
```
###Input sample 4:
Here is a set of inputs. For example:
```in
0 2000 5 12 30
```
###Output sample 4:
The corresponding output is given here. For example:
```out
Wrong Format
```







answer:If there is no answer, please comment
After writing the program, if you want to test the correctness of the three function points, you can refer to the relevant methods in the localdatetime class in java8 (which are not allowed in submitted code)
For example:
```
LocalDateTime today = LocalDateTime.now();// Defining objects
System.out.println(today.plusDays(10));// Ten days after the current date
System.out.println(today.plusDays(-10));// Ten days before current date
```
Note that when period is used to calculate the days of difference between two dates, period can only calculate the days of difference between the dates of the same month, so the following method should be adopted:
```
System.out.println(LocalDate.now().toEpochDay() - LocalDate.now().minusDays(5).toEpochDay());
```