-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCalendar.java
437 lines (395 loc) · 13.2 KB
/
RCalendar.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package bbtrial.nl.logicgate.ace;
/**
* RCalendar is the Calendar for BriefBot. It smooths over several of the
* peculiarities of Java's Calendar, while retaining Calendar's functions.
* RCalendar accepts and presents months in conventional (Jan = 1) rather than
* Java (Jan = 0) counting.
* It also ensures that hours, minutes, seconds, and milliseconds are set to 0.
* @author S. Medlock
*/
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class RCalendar {
private long millis;
/**
* Constructor for a default RCalendar object with today's year, month, and day;
* with hr:min:sec set to OO:OO:OO.
*/
public RCalendar(){
Calendar cal = zeroTime(new GregorianCalendar());
if(BriefBot.TODAY.length == 3){
cal.set(BriefBot.TODAY[0], BriefBot.TODAY[1], BriefBot.TODAY[2]);
}
millis = cal.getTimeInMillis();
}
private Calendar zeroTime(Calendar cal){
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
/**
* Takes a String date in YYYY-mM-dD format and constructs an RCalendar object
* @param String rCal
*/
public RCalendar(String rCal) {
Calendar cal = zeroTime(new GregorianCalendar());
try {
String[] rCalArray = rCal.split("-");
String yearString = rCalArray[0];
int year = Integer.parseInt(yearString);
int month = Integer.parseInt(rCalArray[1]);
int day = Integer.parseInt(rCalArray[2]);
if(yearString.length()==4
&&(month>0&&month<=12)
&&(day>0&&day<=31)){
cal.set(year, month-1, day);}
else{throw new IllegalArgumentException();}
} catch (NumberFormatException e) {
throw new IllegalArgumentException(rCal);
}
millis = cal.getTimeInMillis();
}
/**
* Takes a date as 3 integers (YYYY, mm, dd) and constructs an RCalendar object
* @param year
* @param month
* @param day
*/
public RCalendar(int year, int month, int day){
Calendar cal = zeroTime(new GregorianCalendar());
if((year<=9999 && year>=1000)
&&(month>0 && month<=12)
&&(day>0 && day<=31)){
cal.set(year, month-1, day);}
else{throw new IllegalArgumentException(year+"-"+month+"-"+day);}
millis = cal.getTimeInMillis();
}
/**
* Sets the year, month, and day of the current RCalendar equal
* to the year, month, and day of the Calendar object.
* @param cal
*/
private void setYearMonthDay(Calendar cal) {
Calendar zeroCal = zeroTime(new GregorianCalendar());
zeroCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
zeroCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
zeroCal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
millis = zeroCal.getTimeInMillis();
}
/**
* Construct an RCalendar object from a Calendar object
* @param cal
*/
public RCalendar(Calendar cal) {
setYearMonthDay(cal);
}
/**
* Construct an RCalendar object from another RCalendar object
* @param rCal
*/
public RCalendar(RCalendar rCal){
millis = rCal.getMillis();
}
/**
* Construct and RCalendar object from an SQL Timestamp object
* @param Timestamp
*/
public RCalendar(Timestamp t){
long milliseconds = t.getTime();
Calendar cal = new GregorianCalendar();
cal.clear();
cal.setTime(new java.util.Date(milliseconds));
setYearMonthDay(cal);
}
/**
* Construct an RCalendar which is equal to today's date
* plus some integer number of DATE (days), WEEK_OF_MONTH,
* MONTH, or YEAR. Use negative numbers to subtract.
* @param String d, w, m, or y indicates that the amount
* should be added to days, weeks, months, or years
* @param amount is the number of d,w,m, or y to be added
*/
public RCalendar(String dwmy, int amount){
setYearMonthDay(millisPlusAmount(
zeroTime(new GregorianCalendar()).getTimeInMillis(),
dwmy, amount));
}
/**
* Construct an RCalendar which is equal to the date
* of the parameter r, plus some integer number of
* DATE (days), WEEK_OF_MONTH, MONTH, or YEAR.
* Use negative numbers to subtract.
* @param r
* @param dwmy
* @param amount
*/
public RCalendar(RCalendar r, String dwmy, int amount){
setYearMonthDay(millisPlusAmount(r.getMillis(), dwmy, amount));
}
private Calendar millisPlusAmount(long millis, String dwmy, int amount){
Calendar cal = new GregorianCalendar();
cal.clear();
cal.setTime(new java.util.Date(millis));
if(dwmy.equals("d")){cal.add(Calendar.DATE, amount);}
if(dwmy.equals("w")){cal.add(Calendar.WEEK_OF_YEAR, amount);}
if(dwmy.equals("m")){cal.add(Calendar.MONTH, amount);}
if(dwmy.equals("y")){cal.add(Calendar.YEAR, amount);}
return cal;
}
/**
* Gets the millis value of this RCalendar object, similar to
* Calendar.getTimeInMillis
* @return millis
*/
public long getMillis(){
return millis;
}
public int getYear(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(Calendar.YEAR);
}
public void setYear(int year){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.set(Calendar.YEAR, year);
millis = cal.getTimeInMillis();
}
/**
* Gets the number of the month in usual Jan = 1 form
* @return
*/
public int getRMonth(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return (cal.get(Calendar.MONTH) + 1);
}
/**
* Gets the number of the month as a string
* with a leading zero for single-digit months
* @return
*/
public String getZeroMonth(){
String month = "" + getRMonth();
if(month.length() == 1){month = "0" + month;}
return month;
}
/**
* Sets the number of the month, taking the month
* in usual Jan = 1 numbering
* @param month
*/
public void setRMonth(int month){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.set(Calendar.MONTH, (month-1));
millis = cal.getTimeInMillis();
}
public int getWeekOfMonth(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(Calendar.WEEK_OF_MONTH);
}
public void setWeekOfMonth(int week){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.set(Calendar.WEEK_OF_MONTH, week);
millis = cal.getTimeInMillis();
}
public int getDayOfMonth(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(Calendar.DAY_OF_MONTH);
}
public String getZeroDay(){
String day = "" + getDayOfMonth();
if(day.length() == 1){day = "0" + day;}
return day;
}
public void setDayOfMonth(int day){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.set(Calendar.DAY_OF_MONTH, day);
millis = cal.getTimeInMillis();
}
public int getIntDayOfWeek(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(Calendar.DAY_OF_WEEK);
}
public void setIntDayOfWeek(int intDayOfWeek){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.set(Calendar.DAY_OF_WEEK, intDayOfWeek);
millis = cal.getTimeInMillis();
}
/**
* Gets the English string for the day of the week
* for this RCalendar.
* @return
*/
public String getStringDayOfWeek(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == 1){return "sunday";}
if(dayOfWeek == 2){return "monday";}
if(dayOfWeek == 3){return "tuesday";}
if(dayOfWeek == 4){return "wednesday";}
if(dayOfWeek == 5){return "thursday";}
if(dayOfWeek == 6){return "friday";}
if(dayOfWeek == 7){return "saturday";}
return "";
}
/**
* Sets the day of the week of this RCalendar using
* a String = the English day of the week.
* @param dayOfWeek
*/
public void setStringDayOfWeek(String dayOfWeek){
if(dayOfWeek.equalsIgnoreCase("sunday")){setIntDayOfWeek(1);}
if(dayOfWeek.equalsIgnoreCase("monday")){setIntDayOfWeek(2);}
if(dayOfWeek.equalsIgnoreCase("tuesday")){setIntDayOfWeek(3);}
if(dayOfWeek.equalsIgnoreCase("wednesday")){setIntDayOfWeek(4);}
if(dayOfWeek.equalsIgnoreCase("thursday")){setIntDayOfWeek(5);}
if(dayOfWeek.equalsIgnoreCase("friday")){setIntDayOfWeek(6);}
if(dayOfWeek.equalsIgnoreCase("saturday")){setIntDayOfWeek(7);}
}
/**
* Returns the 3 letter abbreviation for the month
* of this RCalendar instance.
* (standard Dutch abbreviations)
* @return
*/
public String get3LetterMonth(){
if(this.getRMonth()==1){return "jan";}
if(this.getRMonth()==2){return "feb";}
if(this.getRMonth()==3){return "mrt";}
if(this.getRMonth()==4){return "apr";}
if(this.getRMonth()==5){return "mei";}
if(this.getRMonth()==6){return "jun";}
if(this.getRMonth()==7){return "jul";}
if(this.getRMonth()==8){return "aug";}
if(this.getRMonth()==9){return "sep";}
if(this.getRMonth()==10){return "okt";}
if(this.getRMonth()==11){return "nov";}
if(this.getRMonth()==12){return "dec";}
return "";
}
/**
* Adds the specified amount of time to this RCalendar instance.
* Use negative numbers to subtract.
* @param years
* @param months
* @param weeks
* @param days
*/
public void add(int years, int months, int weeks, int days){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
cal.add(Calendar.YEAR, years);
cal.add(Calendar.MONTH, months);
cal.add(Calendar.WEEK_OF_MONTH, weeks);
cal.add(Calendar.DAY_OF_MONTH, days);
millis = cal.getTimeInMillis();
}
/**
* Returns true if the current date-time of this RCalendar is before
* the date-time of the parameter RCalendar.
* @param r
* @return
*/
public boolean before(RCalendar c){
if(millis < c.getMillis()){return true;}
else{return false;}
}
/**
* Returns true if the current date-time of this RCalendar is after
* the date-time of the parameter RCalendar.
* @param r
* @return
*/
public boolean after(RCalendar c){
if(millis > c.getMillis()){return true;}
else{return false;}
}
/**
* Checks if the date-time of this RCalendar is between two other RCalendar date-times.
* It is allowed to be equal to one (inclusive) but not the other (exclusive).
* @param inclusive
* @param exclusive
* @return true if this RCalendar is between the two dates
*/
public boolean betweenOneInclusive(RCalendar inclusive, RCalendar exclusive){
if(this.equals(inclusive)){
return true; //if it is equal to the inclusive date, just return true
}
RCalendar first = null;
RCalendar second = null;
if(inclusive.before(exclusive)){ //if inclusive is before exclusive, inclusive is first
first = inclusive;
second = exclusive;
} else { //else the other way 'round
first = exclusive;
second = inclusive;
}
//if it is after the earlier date and before the later date, it is between the two. Return true.
if(this.after(first) && this.before(second)){
return true;
}
//it is not equal to the inclusive or between the two dates. Return false.
return false;
}
/**
* Returns the number of days which have elapsed between
* the date represented by this RCalendar and today's date.
* @return int
*/
public int daysSince() {
long nowLong = new RCalendar().getMillis();
long elapsedDays = (nowLong - millis)/(1000 * 60 * 60 * 24);
//being RCalendars ensures that they will divide to a whole number of days
return (int) elapsedDays;
}
/**
* Gets a string representation of this RCalendar object
* in DD MMM YYYY format (e.g. 01 Jul 1999)
* @return String
*/
public String getDDMMMYYYY(){
return getZeroDay() + " "
+ get3LetterMonth()
+ " " + getYear();
}
/**
* Overrides equals. Returns true if the time in millis for both
* RCalendar objects is the same (i.e. they represent the same time).
* @param r
* @return boolean
*/
public boolean equals(RCalendar r){
if(this.getMillis() == r.getMillis()){return true;}
else{return false;}
}
/**
* Returns the time represented by this RCalendar as a sql Timestamp object.
* This method was added for the demo.
* @return Timestamp
*/
public Timestamp toTimestamp(){
return Timestamp.valueOf(this.getYear() + "-" + this.getZeroMonth() + "-" + this.getZeroDay() + " 00:00:000");
}
/**
* Overrides toString so that RCalendar objects print in the usual real-world
* year-month-day form. Does not use leading 0's for single-digit months and days.
* @return String YYYY-M(M)-D(D)
*/
public String toString(){
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(millis);
return "" + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
}
}