-
Notifications
You must be signed in to change notification settings - Fork 9
/
Date.java
308 lines (304 loc) · 8.89 KB
/
Date.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//*******************************************************
// Date.java
// The class manages everything related to dates
// Author: liron mizrahi
//*******************************************************
public class Date
{
private int _day; // Number between 1 - 31
private int _month; // Number between 1 - 12
private int _year; // 4 digit number
// Constant in the class
private static final int MAX_YEAR = 9999;
private static final int MIN_YEAR = 1000;
private static final int DEFAULT_YEAR = 2000;
private static final int MAX_MONTH = 12;
private static final int MIN_MONTH = 1;
private static final int MAX_DAY = 31;
private static final int MIN_DAY = 1;
private static final int JANUARY = 1;
private static final int FEBRUARY = 2;
private static final int MARCH = 3;
private static final int APRIL = 4;
private static final int MAY = 5;
private static final int JUNE = 6;
private static final int JULY = 7;
private static final int AUGUST = 8;
private static final int SEPTEMBER = 9;
private static final int OCTOBER = 10;
private static final int NOVEMBER = 11;
private static final int DECEMBER = 12;
private static final int LEAP_FEBRUARY = 29;
/**
* Constructor for class Date
* @param day of the date, month of the date, year of the date
* @return None
*/
public Date(int day, int month, int year)
{
if(isDateValid(day, month, year))
{
this._day = day;
this._month = month;
this._year = year;
}
else
{
this._day = MIN_DAY;
this._month = MIN_MONTH;
this._year = DEFAULT_YEAR;
}
}// end of Date method
/**
* Constructor for class Car
* @param other
* @return None
*/
public Date (Date other)
{
this._day = other._day;
this._month = other._month;
this._year = other._year;
}// end of Date method
/**
* Method return the day of the rent
* @param None
* @return day of the rent
*/
public int getDay()
{
return this._day;
}// end of getDay method
/**
* Method return the month of the rent
* @param None
* @return month of the rent
*/
public int getMonth()
{
return this._month;
}// end of getMonth method
/**
* Method return the year of the rent
* @param None
* @return year of the rent
*/
public int getYear()
{
return this._year;
}// end of getYear method
/**
* Method set the day of the rent
* @param day of the rent ot set
* @return None
*/
public void setDay(int dayToSet)
{
if(isDateValid(dayToSet, this._month, this._year))
{
this._day = dayToSet;
}
}// end of setDay method
/**
* Method set the month of the rent
* @param month of the rent ot set
* @return None
*/
public void setMonth(int monthToSet)
{
if(isDateValid(this._day, monthToSet, this._year))
{
this._month = monthToSet;
}
}// end of setMonth method
/**
* Method set the year of the rent
* @param year of the rent ot set
* @return None
*/
public void setYear(int YearToSet)
{
if(isDateValid(this._day, this._month, YearToSet))
{
this._year = YearToSet;
}
}// end of setYear method
/**
* Method checks if the date received as a parameter is the same as the date represented by the object on which the method is invoked
* two dates are equals if the day, month and year is the same.
* @param other
* @return true if the dates is equals or false if not
*/
public boolean equals (Date other)
{
if (other._day == this._day && other._month == this._month && other._year == this._year)
{
return true;
}
return false;
}// end of equals method
/**
* Method checks if the date represented by the object on which the method is invoked is before the date received as a parameter
* @param other
* @return true if the date is before the other date or false if not
*/
public boolean before (Date other)
{
if ((this._month < other._month && this._year < other._year) || (this._day == other._day && this._month == other._month && this._year < other._year) || (this._month == other._month && this._year == other._year && this._day < other._day) || (this._month > other._month && this._year < other._year) || (this._year == other._year && this._month < other._month))
{
return true;
}
return false;
}// end of before method
/**
* Method checks if the date represented by the object on which the method is invoked is later than the date received as a parameter
* @param other
* @return true if the date is after the other date or false if not
*/
public boolean after(Date other)
{
if (other.before(this) == true)
{
return true;
}
return false;
}// end of after method
/**
* Method calculates and checks the difference in days between the date received as a parameter and the date represented by the object
* @param other
* @return the difference in days between the date received as a parameter and the date represented by the object
*/
public int difference (Date other)
{
int difference = Math.abs(calculateDate(this._day, this._month, this._year) - calculateDate(other._day, other._month, other._year));
return difference;
}// end of difference method
/**
* Method returns the date as a string
* @param None
* @return string of the date
*/
public String toString()
{
String dayAsString;
String monthAsString;
if(this._day < 10)
{
dayAsString = "0" + this._day;
}
else
{
dayAsString = "" + this._day;
}
if(this._month < 10)
{
monthAsString = "0" + this._month;
}
else
{
monthAsString = "" + this._month;
}
String date = dayAsString + "/" + monthAsString + "/" + this._year;
return date;
}// end of toString method
/**
* Method returns object date of tomorrow
* @param None
* @return Date
*/
public Date tomorrow()
{
int tomorrowDay = (daysInMonth(this._month, this._year) == this._day) ? 1 : this._day + 1;
int tomorrowMonth = (tomorrowDay == 1) ? this._month + 1 : this._month;
if ( tomorrowMonth > MAX_MONTH)
{
tomorrowMonth = 1;
}
int tomorrowYear = (tomorrowDay == 1 && tomorrowMonth == 1) ? this._year + 1 : this._year;
return new Date(tomorrowDay, tomorrowMonth, tomorrowYear);
}// end of tomorrow method
/**
* Method check if the date is valid
* @param day of the date, month of the date and the year of the date
* @return true of the date is valid and false if not
*/
public boolean isDateValid(int day, int month, int year)
{
if( day < MIN_DAY || day > MAX_DAY || month < MIN_MONTH || month > MAX_MONTH || year < MIN_YEAR || year > MAX_YEAR)
{
return false;
}
return day <= daysInMonth(month, year);
}// end of isDateValid method
/**
* Method return number of days in month
* @param int
* @return int
*/
private int daysInMonth(int month, int year)
{
switch(month)
{
case JANUARY:
return MAX_DAY;
case MARCH:
return MAX_DAY;
case MAY:
return MAX_DAY;
case JULY:
return MAX_DAY;
case AUGUST:
return MAX_DAY;
case OCTOBER:
return MAX_DAY;
case DECEMBER:
return MAX_DAY;
case APRIL:
return 30;
case JUNE:
return 30;
case SEPTEMBER:
return 30;
case NOVEMBER:
return 30;
case FEBRUARY:
return (isLeapYear(year)) ? LEAP_FEBRUARY: 28;
}
return 0;
}// end of method daysInMonth
/**
* Method return if the year is leap year
* @param int
* @return boolean
*/
public boolean isLeapYear(int year)
{
if (year % 400 == 0)
{
return true;
}
else if (year % 100 == 0)
{
return false;
}
else if (year % 4 == 0)
{
return true;
}
return false;
}// end of method isLeapYear
/**
* Method computes the day number since the beginning of the Christian counting of years
* @param day of the date, month of the date and the year of the date
* @return the day number since the beginning of the Christian counting of years
*/
private int calculateDate( int day, int month, int year)
{
if(month < 3)
{
year--;
month = month + MAX_MONTH;
}
return 365*year + year/4 - year/100 + year/400 +((month+1) * 306)/10 + (day - 62);
}// end of calculateDate method
}// end of class Date