-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.java
More file actions
237 lines (214 loc) · 7.19 KB
/
Date.java
File metadata and controls
237 lines (214 loc) · 7.19 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package HWs;
/* Date.java */
import java.io.*;
class Date {
private int month;
private int day;
private int year;
/* Put your private data fields here. */
/**
* Constructs a date with the given month, day and year. If the date is not
* valid, the entire program will halt with an error message.
*
* @param month is a month, numbered in the range 1...12.
* @param day is between 1 and the number of days in the given month.
* @param year is the year in question, with no digits omitted.
*/
public Date(int month, int day, int year) {
if (isValidDate(month, day, year)) {
this.month = month;
this.day = day;
this.year = year;
} else {
System.out.println("Error: Date entered is invalid");
System.exit(0);
}
}
/**
* Constructs a Date object corresponding to the given string.
*
* @param s should be a string of the form "month/day/year" where month must be
* one or two digits, day must be one or two digits, and year must be
* between 1 and 4 digits. If s does not match these requirements or is
* not a valid date, the program halts with an error message.
*/
public Date(String s) {
String monthTxt = s.substring(0, s.indexOf('/'));
String dayTxt = s.substring(s.indexOf('/') + 1, s.indexOf('/', s.indexOf('/') + 1));
String yearTxt = s.substring(s.indexOf('/', s.indexOf('/', s.indexOf('/') + 1)) + 1, s.length());
if ((dayTxt.length() >= 1 && dayTxt.length() <= 2) && (monthTxt.length() >= 1 && monthTxt.length() <= 2)
&& (yearTxt.length() >= 1 && yearTxt.length() <= 4)
&& (isValidDate(Integer.parseInt(monthTxt), Integer.parseInt(dayTxt), Integer.parseInt(yearTxt)))) {
month = Integer.parseInt(monthTxt);
day = Integer.parseInt(dayTxt);
year = Integer.parseInt(yearTxt);
} else {
System.out.println("Error: Date entered is invalid");
System.exit(0);
}
}
/**
* Checks whether the given year is a leap year.
*
* @return true if and only if the input year is a leap year.
*/
public static boolean isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
/**
* Returns the number of days in a given month.
*
* @param month is a month, numbered in the range 1...12.
* @param year is the year in question, with no digits omitted.
* @return the number of days in the given month.
*/
public static int daysInMonth(int month, int year) {
if (month == 9 || month == 4 || month == 6 || month == 11) {
return 30;
} else if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
} else {
return 31;
}
}
/**
* Checks whether the given date is valid.
*
* @return true if and only if month/day/year constitute a valid date.
*
* Years prior to A.D. 1 are NOT valid.
*/
public static boolean isValidDate(int month, int day, int year) {
return (month >= 1 && month <= 12 && year >= 1 && day >= 1 && day <= daysInMonth(month, year));
}
/**
* Returns a string representation of this date in the form month/day/year. The
* month, day, and year are expressed in full as integers; for example,
* 12/7/2006 or 3/21/407.
*
* @return a String representation of this date.
*/
public String toString() {
return month + "/" + day + "/" + year;
}
/**
* Determines whether this Date is before the Date d.
*
* @return true if and only if this Date is before d.
*/
public boolean isBefore(Date d) {
if (year < d.year) {
return true;
} else if (year == d.year) {
if (month < d.month) {
return true;
} else if (month == d.month) {
return day < d.day;
}
}
return false;
}
/**
* Determines whether this Date is after the Date d.
*
* @return true if and only if this Date is after d.
*/
public boolean isAfter(Date d) {
if (year > d.year) {
return true;
} else if (year == d.year) {
if (month > d.month) {
return true;
} else if (month == d.month) {
return day > d.day;
}
}
return false;
}
/**
* Returns the number of this Date in the year.
*
* @return a number n in the range 1...366, inclusive, such that this Date is
* the nth day of its year. (366 is used only for December 31 in a leap
* year.)
*/
public int dayInYear() {
int count = 0;
for (int i = 1; i < month; i++) {
count += daysInMonth(i, year);
}
count += day;
return count;
}
/**
* Determines the difference in days between d and this Date. For example, if
* this Date is 12/15/2012 and d is 12/14/2012, the difference is 1. If this
* Date occurs before d, the result is negative.
*
* @return the difference in days between d and this date.
*/
public int difference(Date d) {
int count1 = 0, count2 = 0;
if(isAfter(d)) {
for(int i = (year - d.year); i > 0; i--) {
if(isLeapYear(year-i)) {
count1 += 366;
}else {
count1 += 365;
}
}
} else if(isBefore(d)) {
for(int i = (d.year - year); i > 0; i--) {
if(isLeapYear(d.year-i)) {
count2 += 366;
}else {
count2 += 365;
}
}
}
count1 += dayInYear();
count2 += d.dayInYear();
return count1 - count2;
}
public static void main(String[] argv) {
System.out.println("\nTesting constructors.");
Date d1 = new Date(1, 1, 1);
System.out.println("Date should be 1/1/1: " + d1);
d1 = new Date("2/4/2");
System.out.println("Date should be 2/4/2: " + d1);
d1 = new Date("2/29/2000");
System.out.println("Date should be 2/29/2000: " + d1);
d1 = new Date("2/29/1904");
System.out.println("Date should be 2/29/1904: " + d1);
d1 = new Date(12, 31, 1975);
System.out.println("Date should be 12/31/1975: " + d1);
Date d2 = new Date("1/1/1976");
System.out.println("Date should be 1/1/1976: " + d2);
Date d3 = new Date("1/2/1976");
System.out.println("Date should be 1/20/1976: " + d3);
Date d4 = new Date("2/27/1977");
Date d5 = new Date("8/31/2110");
/* I recommend you write code to test the isLeapYear function! */
System.out.println("\nTesting before and after.");
System.out.println(d2 + " after " + d1 + " should be true: " + d2.isAfter(d1));
System.out.println(d3 + " after " + d2 + " should be true: " + d3.isAfter(d2));
System.out.println(d1 + " after " + d1 + " should be false: " + d1.isAfter(d1));
System.out.println(d1 + " after " + d2 + " should be false: " + d1.isAfter(d2));
System.out.println(d2 + " after " + d3 + " should be false: " + d2.isAfter(d3));
System.out.println(d1 + " before " + d2 + " should be true: " + d1.isBefore(d2));
System.out.println(d2 + " before " + d3 + " should be true: " + d2.isBefore(d3));
System.out.println(d1 + " before " + d1 + " should be false: " + d1.isBefore(d1));
System.out.println(d2 + " before " + d1 + " should be false: " + d2.isBefore(d1));
System.out.println(d3 + " before " + d2 + " should be false: " + d3.isBefore(d2));
System.out.println("\nTesting difference.");
System.out.println(d1 + " - " + d1 + " should be 0: " + d1.difference(d1));
System.out.println(d2 + " - " + d1 + " should be 1: " + d2.difference(d1));
System.out.println(d3 + " - " + d1 + " should be 2: " + d3.difference(d1));
System.out.println(d3 + " - " + d4 + " should be -422: " + d3.difference(d4));
System.out.println(d5 + " - " + d4 + " should be 48762: " + d5.difference(d4));
}
}