-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.java
180 lines (163 loc) · 5.09 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
/**
* The Date class defines a date, month, year, and the time.
* It is to be used to store the date of a reservation.
*/
public class Date {
private int day;
private int month;
private int year;
/** Hour which is within the range of 0-23. */
private int hour;
/** Minute which is within the range of 0-59. */
private int minute;
/**
* This constructor creates a Date instance with just a given day, setting the month and year to June 2024.
* @param day Given day.
* @throws IllegalDateException Exception that shows up when the Date is out of bounds
*/
public Date(int day) throws IllegalDateException {
if (day < 1 || day > 31) {
throw new IllegalDateException();
} else {
this.day = day;
this.month = 6;
this.year = 2024;
this.hour = 15;
this.minute = 0;
}
}
/**
* Gets the Date object's day number.
* @return Day of the month.
*/
public int getDay() {
return day;
}
/**
* Gets the Date object's month number.
* @return Month of the year.
*/
public int getMonth() {
return month;
}
/**
* Gets the Date object's year number.
* @return Year number.
*/
public int getYear() {
return year;
}
/**
* Gets the Date object's hour number.
* @return Hour of the day from 0-23.
*/
public int getHour() {
return hour;
}
/**
* Gets the Date object's minute number.
* @return Minute of the hour from 0-59.
*/
public int getMinute() {
return minute;
}
/**
* Gets the time of the Date object expressed as a String.
* @return Formatted String regarding the time.
*/
public String getTime() {
return String.format("%02d", hour) + ":" + String.format("%02d", minute);
}
/**
* getFormattedDate() returns a string containing the date, formatted in YYYY-MM-DD HH:MM
* @return Formatted String regarding the full date.
*/
public String getFormattedDate() {
return getYear() + "-" + getMonth() + "-" + getDay() + " " + String.format("%02d", getHour()) + ":" + String.format("%02d", getMinute());
}
/**
* Sets the hour variable in the Date object.
* @param hour Hour number that the hour variable will be set to.
*/
public void setHour(int hour) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("Invalid hour entered: " + hour);
} else {
this.hour = hour;
}
}
/**
* Sets the minute variable in the Date object.
* @param minute Minute number that the minute variable will be set to.
*/
public void setMinute(int minute) {
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException("Invalid minute entered: " + minute);
} else {
this.minute = minute;
}
}
/**
* getDayDifference() returns the difference between the date object and another given as a parameter in days.
* @param otherDate Other date object to get difference of. (otherDate - this date object).
* @return Difference in days between two dates.
*/
public int getDayDifference(Date otherDate) {
return otherDate.getDay() - getDay();
}
/**
* Checks if this Date object's date is before a given Date object's date.
* @param date Other Date object to compare.
* @return true if this Date object is before the given Date object parameter. False otherwise.
*/
public boolean isBefore(Date date) {
if (date.getYear() < this.year) {
return false;
}
else if (date.getYear() == this.year && date.getMonth() < this.month) {
return false;
}
else if (date.getYear() == this.year && date.getMonth() == this.month && date.getDay() < this.day) {
return false;
}
else if (date.getYear() == this.year && date.getMonth() == this.month && date.getDay() == this.day && date.getHour() < this.hour) {
return false;
}
else if (date.getYear() == this.year && date.getMonth() == this.month && date.getDay() == this.day && date.getHour() == this.hour && date.getMinute() < this.minute) {
return false;
}
return true;
}
/**
* Checks if this Date object's date is after a given Date object's date.
* @param date Other Date object to compare.
* @return true if this Date object is after the given Date object parameter. False otherwise.
*/
public boolean isAfter(Date date) {
if(date.getYear() > this.year) {
return false;
}
else if(date.getYear() == this.year && date.getMonth() > this.month) {
return false;
}
else if(date.getYear() == this.year && date.getMonth() == this.month && date.getDay() > this.day) {
return false;
}
else if(date.getYear() == this.year && date.getMonth() == this.month && date.getDay() == this.day && date.getHour() > this.hour) {
return false;
}
else if(date.getYear() == this.year && date.getMonth() == this.month && date.getDay() == this.day && date.getHour() == this.hour && date.getMinute() > this.minute) {
return false;
}
return true;
}
/**
* isBetween() checks if this Date is in between two other Date instances.
* @param dateBefore The Date before this Date.
* @param dateAfter The Date after this Date.
* @return True if the Date object is before the 2nd date parameter and after the 1st date parameter.
*/
public boolean isBetween(Date dateAfter, Date dateBefore) {
return this.isBefore(dateBefore) && this.isAfter(dateAfter);
}
}