This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day.java
148 lines (130 loc) · 4.46 KB
/
Day.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.library.util;
import java.util.Scanner;
public class Day {
private int date, month, year;
public Day() {
this.date = 0;
this.month = 0;
this.year = 0;
}
public Day(int date, int month, int year) {
this.date = date;
this.month = month;
this.year = year;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}
public static int numOfDays(int month, int year) {
return switch (month) {
case 2 -> isLeapYear(year) ? 29 : 28;
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
default -> 0;
};
}
public static boolean isValidDay(int date, int month, int year) {
return year > 1930 && year <= 2030 &&
month > 0 && month <= 12 &&
date > 0 && date <= numOfDays(month, year);
}
public static boolean isValidDay(Day day) {
return day.year > 1930 && day.year <= 2030 &&
day.month > 0 && day.month <= 12 &&
day.date > 0 && day.date <= numOfDays(day.month, day.year);
}
public static int calculateDays(Day day1, Day day2) {
int days = 0;
Day temp1, temp2;
/*
* temp1 là ngày nhỏ hơn temp2 (ngày nhỏ hơn sẽ gán vào temp1)
* nếu day1.year < day2.year thì gán day1 vào temp1 và ngược lại
* nếu day1.year = day2.year thì xét tháng:
* nếu day1.month < day2.month thì gán day1 vào temp1 và ngược lại
* nếu day1.month = day2.month thì xét ngày:
* nếu day1.date = day2.date thì gán day1 vào temp1 và ngược lại
*/
if (day1.year < day2.year ||
day1.year == day2.year && day1.month < day2.month ||
day1.year == day2.year && day1.month == day2.month && day1.date < day2.date) {
temp1 = new Day(day1.date, day1.month, day1.year);
temp2 = new Day(day2.date, day2.month, day2.year);
} else {
temp1 = new Day(day2.date, day2.month, day2.year);
temp2 = new Day(day1.date, day1.month, day1.year);
}
while (temp1.year <= temp2.year) {
if (temp1.year == temp2.year && temp1.month == temp2.month) {
days += temp2.date - temp1.date;
break;
} else {
days += numOfDays(temp1.month, temp2.year) - temp1.date;
temp1.month++;
temp1.date = 0;
if (temp1.month > 12) {
temp1.year++;
temp1.month = 1;
}
}
}
return days;
}
public static Day inputDay(String message) {
Scanner sc = new Scanner(System.in);
Day day = new Day();
boolean hasError;
do {
hasError = false;
System.out.print(message);
String input = sc.nextLine();
String[] splitInput = input.split("/");
try {
day.setDate(Integer.parseInt(splitInput[0]));
day.setMonth(Integer.parseInt(splitInput[1]));
day.setYear(Integer.parseInt(splitInput[2]));
} catch (Exception e) {
hasError = true;
}
} while (hasError || !Day.isValidDay(day));
return day;
}
public static Day parseDay(String str) throws Exception {
String[] temp = str.split("/");
int date, month, year;
try {
date = Integer.parseInt(temp[0]);
month = Integer.parseInt(temp[1]);
year = Integer.parseInt(temp[2]);
if (!Day.isValidDay(date, month, year))
throw new Exception();
} catch (Exception e) {
throw new Exception();
}
return new Day(date, month, year);
}
@Override
public String toString() {
// dd/mm/yyyy
return ((date < 10) ? "0" : "") + date + "/" +
((month < 10) ? "0" : "") + month + "/" +
year;
}
}