1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.date;
12
13 import java.util.Calendar;
14 import java.util.Date;
15 import java.util.GregorianCalendar;
16 import java.util.Locale;
17 import java.util.TimeZone;
18
19 import de.jaret.util.misc.PropertyObservableBase;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class JaretDate extends PropertyObservableBase implements Comparable<JaretDate> {
36
37 public static final long MILLIS_PER_HOUR = 60 * 60 * 1000;
38
39 public static final long MILLIS_PER_MINUTE = 60 * 1000;
40
41 public static final long MILLIS_PER_SECOND = 1000;
42
43
44
45
46 private static JaretDateFormatter _jaretDateFormatter = new JaretDateFormatter();
47
48
49 private Date _date;
50
51 private Calendar _calendar;
52
53
54
55
56 public JaretDate() {
57 _date = new Date();
58 }
59
60
61
62
63
64
65 public JaretDate(Date date) {
66 _date = (Date) date.clone();
67 }
68
69
70
71
72
73
74 public JaretDate(String millisecondsString) {
75 long ms = Long.parseLong(millisecondsString);
76 _date = new Date(ms);
77 }
78
79
80
81
82
83
84 public JaretDate(long milliseconds) {
85 long ms = milliseconds;
86 _date = new Date(ms);
87 }
88
89
90
91
92
93
94 public JaretDate(JaretDate jdate) {
95 _date = new Date(jdate.getDate().getTime());
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public JaretDate(int day, int month, int year, int hours, int minutes, int seconds) {
109 this();
110 setDateTime(day, month, year, hours, minutes, seconds);
111 }
112
113
114
115
116
117
118 public JaretDateFormatter getJaretDateFormatter() {
119 return _jaretDateFormatter;
120 }
121
122
123
124
125
126
127
128 public static void setJaretDateFormatter(JaretDateFormatter jaretDateFormatter) {
129
130 _jaretDateFormatter = jaretDateFormatter;
131
132
133
134
135 }
136
137
138
139
140
141
142 public void setLocale(Locale locale) {
143 _jaretDateFormatter.setLocale(locale);
144 firePropertyChange();
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 public void setDateTime(int day, int month, int year, int hours, int minutes, int seconds, int milliseconds) {
159 Calendar cal = getCalendar();
160 cal.set(Calendar.DAY_OF_MONTH, day);
161 cal.set(Calendar.MONTH, month - 1);
162 cal.set(Calendar.YEAR, year);
163 cal.set(Calendar.HOUR_OF_DAY, hours);
164 cal.set(Calendar.MINUTE, minutes);
165 cal.set(Calendar.SECOND, seconds);
166 cal.set(Calendar.MILLISECOND, milliseconds);
167 _date.setTime(cal.getTimeInMillis());
168 firePropertyChange();
169 }
170
171
172
173
174
175
176
177
178
179
180
181 public void setDateTime(int day, int month, int year, int hours, int minutes, int seconds) {
182 setDateTime(day, month, year, hours, minutes, seconds, 0);
183 }
184
185
186
187
188
189
190
191
192
193
194 public JaretDate setTime(int hours, int minutes, int seconds, int milliseconds) {
195 Calendar cal = getCalendar();
196 cal.set(Calendar.HOUR_OF_DAY, hours);
197 cal.set(Calendar.MINUTE, minutes);
198 cal.set(Calendar.SECOND, seconds);
199 cal.set(Calendar.MILLISECOND, milliseconds);
200 _date.setTime(cal.getTimeInMillis());
201 firePropertyChange();
202 return this;
203 }
204
205
206
207
208
209
210
211
212
213 public JaretDate setTime(int hours, int minutes, int seconds) {
214 setTime(hours, minutes, seconds, 0);
215 return this;
216 }
217
218
219
220
221
222
223
224
225
226 public JaretDate setDate(int day, int month, int year) {
227 Calendar cal = getCalendar();
228 cal.set(Calendar.DAY_OF_MONTH, day);
229 cal.set(Calendar.MONTH, month - 1);
230 cal.set(Calendar.YEAR, year);
231 _date.setTime(cal.getTimeInMillis());
232 firePropertyChange();
233 return this;
234 }
235
236
237
238
239
240
241
242 public JaretDate setDate(JaretDate date) {
243 setDate(date.getDay(), date.getMonth(), date.getYear());
244 return this;
245 }
246
247
248
249
250
251
252
253
254 public void setMonth(int month) {
255 Calendar cal = getCalendar();
256 cal.set(Calendar.MONTH, month - 1);
257 _date.setTime(cal.getTimeInMillis());
258 firePropertyChange();
259 }
260
261
262
263
264
265
266 public void setDay(int day) {
267 Calendar cal = getCalendar();
268 cal.set(Calendar.DAY_OF_MONTH, day);
269 _date.setTime(cal.getTimeInMillis());
270 firePropertyChange();
271 }
272
273
274
275
276
277
278 public void setHours(int hours) {
279 if (hours > 23) {
280 throw new IllegalArgumentException("hour must not exceedd 23");
281 }
282 Calendar cal = getCalendar();
283 cal.set(Calendar.HOUR_OF_DAY, hours);
284 _date.setTime(cal.getTimeInMillis());
285 firePropertyChange();
286 }
287
288
289
290
291
292
293 public void setMinutes(int minutes) {
294 if (minutes > 59) {
295 throw new IllegalArgumentException("minutes must not exceed 59");
296 }
297 Calendar cal = getCalendar();
298 cal.set(Calendar.MINUTE, minutes);
299 _date.setTime(cal.getTimeInMillis());
300 firePropertyChange();
301 }
302
303
304
305
306
307
308 public void setSeconds(int seconds) {
309 if (seconds > 59) {
310 throw new IllegalArgumentException("seconds must not exceed 59");
311 }
312 Calendar cal = getCalendar();
313 cal.set(Calendar.SECOND, seconds);
314 cal.set(Calendar.MILLISECOND, 0);
315 _date.setTime(cal.getTimeInMillis());
316 firePropertyChange();
317 }
318
319
320
321
322
323
324 public void setMilliseconds(int milliseconds) {
325 if (milliseconds > 999) {
326 throw new IllegalArgumentException("milliseconds must not exceed 999");
327 }
328 Calendar cal = getCalendar();
329 cal.set(Calendar.MILLISECOND, milliseconds);
330 _date.setTime(cal.getTimeInMillis());
331 firePropertyChange();
332 }
333
334
335
336
337
338
339
340
341 public boolean setDateByText(String text) {
342 Date date = _jaretDateFormatter.parseTextualDate(text);
343 if (date == null) {
344 return false;
345 } else {
346 _date = date;
347 firePropertyChange();
348 return true;
349 }
350 }
351
352
353
354
355
356
357 protected Calendar getCalendar() {
358 if (_calendar == null) {
359 _calendar = Calendar.getInstance(_jaretDateFormatter.getLocale());
360 }
361 _calendar.setTime(_date);
362 return _calendar;
363 }
364
365
366
367
368
369
370 public long getMillisInDay() {
371 Calendar cal = getCalendar();
372 long h = cal.get(Calendar.HOUR_OF_DAY);
373 long m = cal.get(Calendar.MINUTE);
374 long s = cal.get(Calendar.SECOND);
375 return h * MILLIS_PER_HOUR + m * MILLIS_PER_MINUTE + s * MILLIS_PER_SECOND;
376 }
377
378
379
380
381
382
383 public Date getDate() {
384 return _date;
385 }
386
387
388
389
390
391
392 public String toDisplayString() {
393 return _jaretDateFormatter.generateDisplayString(this);
394 }
395
396
397
398
399
400
401 public String toDisplayStringDate() {
402 return _jaretDateFormatter.generateDisplayStringDate(this);
403 }
404
405
406
407
408
409
410
411 public String toDisplayStringTime(boolean seconds) {
412 return _jaretDateFormatter.toDisplayStringTime(this, seconds);
413 }
414
415
416
417
418
419 public String toDisplayStringTime() {
420 return toDisplayStringTime(false);
421 }
422
423
424
425
426
427
428 public String toString() {
429 return Long.toString(_date.getTime());
430 }
431
432
433
434
435 public void setToNow() {
436 _date.setTime(System.currentTimeMillis());
437 firePropertyChange();
438 }
439
440
441
442
443
444
445
446 public double diffMinutes(JaretDate date) {
447 return ((double) diffMilliSeconds(date)) / MILLIS_PER_MINUTE;
448 }
449
450
451
452
453
454
455
456 public int diffSeconds(JaretDate date) {
457 return (int) ((_date.getTime() - date.getDate().getTime()) / MILLIS_PER_SECOND);
458 }
459
460
461
462
463
464
465
466 public long diffSecondsL(JaretDate date) {
467 return (long) ((_date.getTime() - date.getDate().getTime()) / MILLIS_PER_SECOND);
468 }
469
470
471
472
473
474
475
476 public long diffMilliSeconds(JaretDate date) {
477 return _date.getTime() - date.getDate().getTime();
478 }
479
480
481
482
483
484
485
486 public JaretDate advanceMonths(int months) {
487 Calendar cal = getCalendar();
488 cal.add(Calendar.MONTH, months);
489 _date.setTime(cal.getTimeInMillis());
490 firePropertyChange();
491 return this;
492 }
493
494
495
496
497
498
499
500 public JaretDate advanceYears(int years) {
501 Calendar cal = getCalendar();
502 cal.add(Calendar.YEAR, years);
503 _date.setTime(cal.getTimeInMillis());
504 firePropertyChange();
505 return this;
506 }
507
508
509
510
511
512
513
514 public JaretDate advanceDays(int days) {
515 Calendar cal = getCalendar();
516 cal.add(Calendar.DAY_OF_YEAR, days);
517 _date.setTime(cal.getTimeInMillis());
518 firePropertyChange();
519 return this;
520 }
521
522
523
524
525
526
527
528 public JaretDate advanceDays(double days) {
529 _date.setTime(_date.getTime() + ((long) (days * 24.0 * 60.0 * 60.0 * 1000.0)));
530 firePropertyChange();
531 return this;
532 }
533
534 public JaretDate backDays(double days) {
535 _date.setTime(_date.getTime() - ((long) (days * 24.0 * 60.0 * 60.0 * 1000.0)));
536 firePropertyChange();
537 return this;
538 }
539
540 public JaretDate advanceHours(double hours) {
541 _date.setTime(_date.getTime() + ((long) (hours * 60.0 * 60.0 * 1000.0)));
542 firePropertyChange();
543 return this;
544 }
545
546 public JaretDate backHours(double hours) {
547 _date.setTime(_date.getTime() - ((long) (hours * 60.0 * 60.0 * 1000.0)));
548 firePropertyChange();
549 return this;
550 }
551
552 public JaretDate advanceMinutes(double minutes) {
553 _date.setTime(_date.getTime() + ((long) (minutes * 60.0 * 1000.0)));
554 firePropertyChange();
555 return this;
556 }
557
558 public JaretDate backMinutes(double minutes) {
559 _date.setTime(_date.getTime() - ((long) (minutes * 60.0 * 1000.0)));
560 firePropertyChange();
561 return this;
562 }
563
564 public JaretDate advanceSeconds(double seconds) {
565 _date.setTime(_date.getTime() + ((long) (seconds * 1000.0)));
566 firePropertyChange();
567 return this;
568 }
569
570 public JaretDate advanceMillis(long millis) {
571 _date.setTime(_date.getTime() + millis);
572 firePropertyChange();
573 return this;
574 }
575
576 public JaretDate backSeconds(double seconds) {
577 _date.setTime(_date.getTime() - ((long) (seconds * 1000.0)));
578 firePropertyChange();
579 return this;
580 }
581
582
583
584
585 public boolean equals(Object o) {
586 return _date.equals(((JaretDate) o).getDate());
587 }
588
589
590
591
592 public int hashCode() {
593 return _date.hashCode();
594 }
595
596
597
598
599
600
601
602 public int compareTo(JaretDate date) {
603 return _date.compareTo(date._date);
604 }
605
606
607
608
609
610
611
612 public long compareTimeTo(JaretDate date) {
613 return getMillisInDay() - date.getMillisInDay();
614 }
615
616
617
618
619
620
621
622 public int compareDateTo(JaretDate date) {
623 int result;
624 result = getYear() - date.getYear();
625 if (result == 0) {
626 result = getDayOfYear() - date.getDayOfYear();
627 }
628 return result;
629 }
630
631
632
633
634
635
636 public JaretDate copy() {
637 return new JaretDate(this);
638 }
639
640
641
642
643
644
645 public int getMonth() {
646 Calendar cal = getCalendar();
647 return cal.get(Calendar.MONTH) + 1;
648 }
649
650
651
652
653
654
655 public String getMonthString() {
656 return _jaretDateFormatter.getMonthString(getMonth() - 1);
657 }
658
659
660
661
662
663
664 public String getShortMonthString() {
665 return _jaretDateFormatter.getShortMonthString(getMonth() - 1);
666 }
667
668 public int getWeekOfYear() {
669 Calendar cal = getCalendar();
670 return cal.get(Calendar.WEEK_OF_YEAR);
671 }
672
673
674
675
676
677
678 public String getDayOfWeekString() {
679 return _jaretDateFormatter.getDayOfWeekString(getDayOfWeek());
680 }
681
682 public String getShortDayOfWeekString() {
683 return _jaretDateFormatter.getShortDayOfWeekString(getDayOfWeek());
684 }
685
686
687
688
689
690
691 public int getDayOfWeek() {
692 Calendar cal = getCalendar();
693 return cal.get(Calendar.DAY_OF_WEEK);
694 }
695
696 public boolean isWeekendDay() {
697 return getDayOfWeek() == Calendar.SATURDAY || getDayOfWeek() == Calendar.SUNDAY;
698 }
699
700 public int getDay() {
701 Calendar cal = getCalendar();
702 return cal.get(Calendar.DAY_OF_MONTH);
703 }
704
705 public int getHours() {
706 Calendar cal = getCalendar();
707 return cal.get(Calendar.HOUR_OF_DAY);
708 }
709
710 public int getMinutes() {
711 Calendar cal = getCalendar();
712 return cal.get(Calendar.MINUTE);
713 }
714
715 public int getSeconds() {
716 Calendar cal = getCalendar();
717 return cal.get(Calendar.SECOND);
718 }
719
720 public int getMillis() {
721 Calendar cal = getCalendar();
722 return cal.get(Calendar.MILLISECOND);
723 }
724
725 public int getDayOfYear() {
726 Calendar cal = getCalendar();
727 return cal.get(Calendar.DAY_OF_YEAR);
728 }
729
730
731
732
733
734
735 public int getYear() {
736 Calendar cal = getCalendar();
737 return cal.get(Calendar.YEAR);
738 }
739
740 private void firePropertyChange() {
741 firePropertyChange("date", null, this);
742 }
743
744
745
746
747
748 public void clearTime() {
749 setHours(0);
750 setMinutes(0);
751 setSeconds(0);
752 setMilliseconds(0);
753 }
754
755
756
757
758
759
760
761
762 public static JaretDate getFirstDayOfAWeek(int week, int year) {
763 Calendar cal = new GregorianCalendar();
764 cal.set(Calendar.YEAR, year);
765 cal.set(Calendar.WEEK_OF_YEAR, week);
766 cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
767 JaretDate d = new JaretDate(cal.getTime());
768 d.clearTime();
769 return d;
770 }
771
772
773 }