1 package com.atlassian.core.util;
2
3 import com.opensymphony.util.TextUtils;
4 import org.apache.log4j.Category;
5
6 import java.math.BigDecimal;
7 import java.sql.Timestamp;
8 import java.text.DateFormat;
9 import java.text.SimpleDateFormat;
10 import java.util.Calendar;
11 import java.util.Date;
12 import java.util.MissingResourceException;
13 import java.util.ResourceBundle;
14 import java.util.StringTokenizer;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 public class DateUtils
19 {
20 private static Category log = Category.getInstance(DateUtils.class);
21
22
23
24
25
26 private static final Pattern DURATION_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?|\\.\\d+)(.+)");
27
28 public enum Duration
29 {
30 SECOND(1),
31 MINUTE(60),
32 HOUR(60 * MINUTE.getSeconds()),
33 DAY(24 * HOUR.getSeconds())
34 {
35 @Override
36 public long getModifiedSeconds(final long secondsPerDay, final long secondsPerWeek)
37 {
38 return secondsPerDay;
39 }
40 },
41 WEEK(7 * DAY.getSeconds())
42 {
43 @Override
44 public long getModifiedSeconds(final long secondsPerDay, final long secondsPerWeek)
45 {
46 return secondsPerWeek;
47 }
48 },
49 MONTH(31 * DAY.getSeconds())
50 {
51
52
53 @Override
54 public long getModifiedSeconds(final long secondsPerDay, final long secondsPerWeek)
55 {
56 return 31 * secondsPerDay;
57 }
58 },
59 YEAR(52 * WEEK.getSeconds())
60 {
61
62
63 @Override
64 public long getModifiedSeconds(final long secondsPerDay, final long secondsPerWeek)
65 {
66 return 52 * secondsPerWeek;
67 }
68 };
69
70 public long getSeconds()
71 {
72 return seconds;
73 }
74
75 public long getMilliseconds()
76 {
77 return 1000L * getSeconds();
78 }
79
80
81
82
83
84
85
86 public long getModifiedSeconds(final long secondsPerDay, final long secondsPerWeek)
87 {
88 return getSeconds();
89 }
90
91 private final long seconds;
92
93 private Duration(final long seconds)
94 {
95 this.seconds = seconds;
96 }
97 }
98
99
100 public static final long SECOND_MILLIS = Duration.SECOND.getMilliseconds();
101 public static final long MINUTE_MILLIS = Duration.MINUTE.getMilliseconds();
102 public static final long HOUR_MILLIS = Duration.HOUR.getMilliseconds();
103 public static final long DAY_MILLIS = Duration.DAY.getMilliseconds();
104 public static final long MONTH_MILLIS = Duration.MONTH.getMilliseconds();
105 public static final long YEAR_MILLIS = Duration.YEAR.getMilliseconds();
106
107 public static final String AM = "am";
108 public static final String PM = "pm";
109
110
111
112
113
114
115 private static final int[] CALENDAR_PERIODS = { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH,
116 Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND };
117
118
119 private final ResourceBundle resourceBundle;
120
121 public static class DateRange
122 {
123 public final java.util.Date startDate;
124 public final java.util.Date endDate;
125
126 public DateRange(java.util.Date startDate, java.util.Date endDate)
127 {
128 this.startDate = startDate;
129 this.endDate = endDate;
130 }
131 }
132
133
134 public DateUtils(ResourceBundle resourceBundle)
135 {
136 this.resourceBundle = resourceBundle;
137 }
138
139
140
141
142
143
144 public static boolean equalTimestamps(Timestamp t1, Timestamp t2)
145 {
146 return (Math.abs(t1.getTime() - t2.getTime()) < 10L);
147 }
148
149
150 public static final DateFormat ISO8601DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
151
152 public String dateDifferenceBean(long dateA, long dateB, long resolution, ResourceBundle resourceBundle)
153 {
154 return dateDifference(dateA, dateB, resolution, resourceBundle);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public static String dateDifference(long dateA, long dateB, long resolution, ResourceBundle resourceBundle)
172 {
173 long months, days, hours, minutes, seconds;
174 StringBuilder sb = new StringBuilder();
175 long difference = Math.abs(dateB - dateA);
176
177 resolution--;
178 months = difference / Duration.MONTH.getMilliseconds();
179 if (months > 0)
180 {
181 difference = difference % Duration.MONTH.getMilliseconds();
182 if (months > 1)
183 {
184 sb.append(months).append(" ").append(getText(resourceBundle, "core.dateutils.months")).append(", ");
185 }
186 else
187 {
188 sb.append(months).append(" ").append(getText(resourceBundle, "core.dateutils.month")).append(", ");
189 }
190 }
191
192 if (resolution < 0)
193 {
194 if (sb.length() == 0)
195 {
196 return "0 " + getText(resourceBundle, "core.dateutils.months");
197 }
198 else
199 {
200 return sb.substring(0, sb.length() - 2);
201 }
202 }
203 else
204 {
205 resolution--;
206 days = difference / Duration.DAY.getMilliseconds();
207 if (days > 0)
208 {
209 difference = difference % Duration.DAY.getMilliseconds();
210 if (days > 1)
211 {
212 sb.append(days).append(" ").append(getText(resourceBundle, "core.dateutils.days")).append(", ");
213 }
214 else
215 {
216 sb.append(days).append(" ").append(getText(resourceBundle, "core.dateutils.day")).append(", ");
217 }
218 }
219 }
220
221 if (resolution < 0)
222 {
223 if (sb.length() == 0)
224 {
225 return "0 " + getText(resourceBundle, "core.dateutils.days");
226 }
227 else
228 {
229 return sb.substring(0, sb.length() - 2);
230 }
231 }
232 else
233 {
234 resolution--;
235 hours = difference / Duration.HOUR.getMilliseconds();
236 if (hours > 0)
237 {
238 difference = difference % Duration.HOUR.getMilliseconds();
239 if (hours > 1)
240 {
241 sb.append(hours).append(" ").append(getText(resourceBundle, "core.dateutils.hours")).append(", ");
242 }
243 else
244 {
245 sb.append(hours).append(" ").append(getText(resourceBundle, "core.dateutils.hour")).append(", ");
246 }
247 }
248 }
249
250 if (resolution < 0)
251 {
252 if (sb.length() == 0)
253 {
254 return "0 " + getText(resourceBundle, "core.dateutils.hours");
255 }
256 else
257 {
258 return sb.substring(0, sb.length() - 2);
259 }
260 }
261 else
262 {
263 resolution--;
264 minutes = difference / Duration.MINUTE.getMilliseconds();
265 if (minutes > 0)
266 {
267 difference = difference % Duration.MINUTE.getMilliseconds();
268 if (minutes > 1)
269 {
270 sb.append(minutes).append(" ").append(getText(resourceBundle, "core.dateutils.minutes")).append(", ");
271 }
272 else
273 {
274 sb.append(minutes).append(" ").append(getText(resourceBundle, "core.dateutils.minute")).append(", ");
275 }
276 }
277 }
278
279 if (resolution < 0)
280 {
281 if (sb.length() == 0)
282 {
283 return "0 " + getText(resourceBundle, "core.dateutils.minutes");
284 }
285 else
286 {
287 return sb.substring(0, sb.length() - 2);
288 }
289 }
290 else
291 {
292 resolution--;
293 seconds = difference / Duration.SECOND.getMilliseconds();
294 if (seconds > 0)
295 {
296 if (seconds > 1)
297 {
298 sb.append(seconds).append(" ").append(getText(resourceBundle, "core.dateutils.seconds")).append(", ");
299 }
300 else
301 {
302 sb.append(seconds).append(" ").append(getText(resourceBundle, "core.dateutils.second")).append(", ");
303 }
304 }
305 }
306
307 if (resolution <= 0 && sb.length() == 0)
308 {
309 return "0 " + getText(resourceBundle, "core.dateutils.seconds");
310 }
311
312 if (sb.length() > 2)
313 {
314 return sb.substring(0, sb.length() - 2);
315 }
316 else
317 {
318 return "";
319 }
320 }
321
322
323 public static String formatDateISO8601(Date ts)
324 {
325 return ISO8601DateFormat.format(ts);
326 }
327
328
329
330
331
332
333
334 public static boolean validDuration(String s)
335 {
336 try
337 {
338 getDuration(s);
339 return true;
340 }
341 catch (InvalidDurationException e)
342 {
343 return false;
344 }
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 public static long getDuration(String durationStr) throws InvalidDurationException
366 {
367 return getDuration(durationStr, Duration.MINUTE);
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 public static long getDuration(final String durationStr, final Duration defaultUnit) throws InvalidDurationException
386 {
387 return getDurationSeconds(durationStr, Duration.DAY.getSeconds(), Duration.WEEK.getSeconds(), defaultUnit);
388 }
389
390
391
392
393
394
395
396
397
398
399
400 public static long getDuration(String durationStr, int hoursPerDay, int daysPerWeek) throws InvalidDurationException
401 {
402 return getDuration(durationStr, hoursPerDay, daysPerWeek, Duration.MINUTE);
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416 public static long getDuration(String durationStr, int hoursPerDay, int daysPerWeek, final Duration defaultUnit) throws InvalidDurationException
417 {
418 long secondsInDay = hoursPerDay * Duration.HOUR.getSeconds();
419 long secondsPerWeek = daysPerWeek * secondsInDay;
420 return getDurationSeconds(durationStr, secondsInDay, secondsPerWeek, defaultUnit);
421 }
422
423
424
425
426
427
428
429
430
431
432 public static long getDurationWithNegative(String durationStr) throws InvalidDurationException
433 {
434 String cleanedDurationStr = TextUtils.noNull(durationStr).trim();
435 if (!TextUtils.stringSet(cleanedDurationStr))
436 {
437 return 0;
438 }
439
440 boolean negative = false;
441
442 if (cleanedDurationStr.charAt(0) == '-')
443 {
444 negative = true;
445 }
446
447 if (negative)
448 {
449 return 0 - getDuration(cleanedDurationStr.substring(1));
450 }
451 else
452 {
453 return getDuration(cleanedDurationStr);
454 }
455 }
456
457
458
459
460
461
462
463
464
465
466
467
468 public static long getDurationSeconds(String durationStr, long secondsPerDay, long secondsPerWeek, final Duration defaultUnit)
469 throws InvalidDurationException
470 {
471 long time = 0;
472
473 if (org.apache.commons.lang.StringUtils.isBlank(durationStr))
474 {
475 return 0;
476 }
477
478 durationStr = durationStr.trim().toLowerCase();
479
480
481 if (durationStr.indexOf(" ") > 0)
482 {
483 StringTokenizer st = new StringTokenizer(durationStr, ", ");
484 while (st.hasMoreTokens())
485 {
486 time += getDurationSeconds(st.nextToken(), secondsPerDay, secondsPerWeek, defaultUnit);
487 }
488 }
489 else
490 {
491 try
492 {
493 time = Long.parseLong(durationStr.trim()) * defaultUnit.getModifiedSeconds(secondsPerDay, secondsPerWeek);
494 }
495 catch (Exception ex)
496 {
497 final Matcher matcher = DURATION_PATTERN.matcher(durationStr);
498 if(matcher.matches())
499 {
500
501 final String numberAsString = matcher.group(1);
502 final BigDecimal number = new BigDecimal(numberAsString);
503
504 final long unit = getUnit(matcher.group(2), secondsPerDay, secondsPerWeek);
505 final BigDecimal seconds = number.multiply(BigDecimal.valueOf(unit));
506 try
507 {
508
509
510
511
512
513
514
515
516
517
518
519
520 seconds.divide(BigDecimal.valueOf(60)).intValueExact();
521
522
523 time = seconds.intValueExact();
524
525 }
526 catch (ArithmeticException e)
527 {
528 throw new InvalidDurationException("Specified decimal fraction duration cannot maintain precision", e);
529 }
530 }
531 else
532 {
533 throw new InvalidDurationException("Unable to parse duration string: " + durationStr);
534 }
535 }
536 }
537 return time;
538 }
539
540
541
542
543
544
545
546
547
548 private static long getUnit(final String unit, final long secondsPerDay, final long secondsPerWeek) throws InvalidDurationException
549 {
550 long time;
551 switch ((int) unit.charAt(0))
552 {
553 case(int) 'm':
554 validateDurationUnit(unit.substring(0), Duration.MINUTE);
555 time = Duration.MINUTE.getSeconds();
556 break;
557 case(int) 'h':
558 validateDurationUnit(unit.substring(0), Duration.HOUR);
559 time = Duration.HOUR.getSeconds();
560 break;
561 case(int) 'd':
562 validateDurationUnit(unit.substring(0), Duration.DAY);
563 time = secondsPerDay;
564 break;
565 case(int) 'w':
566 validateDurationUnit(unit.substring(0), Duration.WEEK);
567 time = secondsPerWeek;
568 break;
569 default:
570 throw new InvalidDurationException("Not a valid duration string");
571 }
572 return time;
573 }
574
575 private static String validateDurationUnit(final String durationString, final Duration duration)
576 throws InvalidDurationException
577 {
578
579 if (durationString.length() > 1)
580 {
581 String singular = duration.name().toLowerCase();
582 String plural = duration.name().toLowerCase() + "s";
583
584 if (durationString.contains(plural))
585 {
586 return durationString.substring(durationString.indexOf(plural));
587 }
588 else if (durationString.contains(singular))
589 {
590 return durationString.substring(durationString.indexOf(singular));
591 }
592 else
593 {
594 throw new InvalidDurationException("Not a valid durationString string");
595 }
596 }
597 return durationString.substring(1);
598 }
599
600
601
602
603
604
605
606
607
608
609
610 public static String getDurationString(long seconds)
611 {
612 return getDurationStringSeconds(seconds, Duration.DAY.getSeconds(), Duration.WEEK.getSeconds());
613 }
614
615
616
617
618
619
620
621
622
623 public static String getDurationStringWithNegative(long seconds)
624 {
625 if (seconds < 0)
626 {
627 return "-" + getDurationString(-seconds);
628 }
629 else
630 {
631 return getDurationString(seconds);
632 }
633 }
634
635
636
637
638
639
640
641
642
643 public static String getDurationString(long l, int hoursPerDay, int daysPerWeek)
644 {
645 long secondsInDay = hoursPerDay * Duration.HOUR.getSeconds();
646 long secondsPerWeek = daysPerWeek * secondsInDay;
647 return getDurationStringSeconds(l, secondsInDay, secondsPerWeek);
648 }
649
650
651
652
653
654
655
656
657
658
659 public static String getDurationStringSeconds(long l, long secondsPerDay, long secondsPerWeek)
660 {
661 if (l == 0)
662 {
663 return "0m";
664 }
665
666 StringBuilder result = new StringBuilder();
667
668 if (l >= secondsPerWeek)
669 {
670 result.append((l / secondsPerWeek));
671 result.append("w ");
672 l = l % secondsPerWeek;
673 }
674
675 if (l >= secondsPerDay)
676 {
677 result.append((l / secondsPerDay));
678 result.append("d ");
679 l = l % secondsPerDay;
680 }
681
682 if (l >= Duration.HOUR.getSeconds())
683 {
684 result.append((l / Duration.HOUR.getSeconds()));
685 result.append("h ");
686 l = l % Duration.HOUR.getSeconds();
687 }
688
689 if (l >= Duration.MINUTE.getSeconds())
690 {
691 result.append((l / Duration.MINUTE.getSeconds()));
692 result.append("m ");
693 }
694
695 return result.toString().trim();
696 }
697
698
699
700
701
702
703
704
705
706
707
708 public static String getDurationPretty(long numSecs, ResourceBundle resourceBundle)
709 {
710 return getDurationPrettySeconds(numSecs, Duration.DAY.getSeconds(), Duration.WEEK.getSeconds(), resourceBundle, false);
711 }
712
713
714
715
716
717
718
719
720
721
722
723
724
725 public static String getDurationPretty(long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)
726 {
727 long secondsInDay = hoursPerDay * Duration.HOUR.getSeconds();
728 long secondsPerWeek = daysPerWeek * secondsInDay;
729 return getDurationPrettySeconds(numSecs, secondsInDay, secondsPerWeek, resourceBundle, false);
730 }
731
732
733
734
735
736
737
738
739
740
741 public static String getDurationPrettySecondsResolution(long numSecs, ResourceBundle resourceBundle)
742 {
743 return getDurationPrettySeconds(numSecs, Duration.DAY.getSeconds(), Duration.WEEK.getSeconds(), resourceBundle, true);
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757 public static String getDurationPrettySecondsResolution(long numSecs, int hoursPerDay, int daysPerWeek, ResourceBundle resourceBundle)
758 {
759 long secondsInDay = hoursPerDay * Duration.HOUR.getSeconds();
760 long secondsPerWeek = daysPerWeek * secondsInDay;
761 return getDurationPrettySeconds(numSecs, secondsInDay, secondsPerWeek, resourceBundle, true);
762 }
763
764
765
766
767
768
769
770
771
772
773 private static String getDurationPrettySeconds(long numSecs, long secondsPerDay, long secondsPerWeek, ResourceBundle resourceBundle, boolean secondsDuration)
774 {
775
776
777 final long secondsPerYear = secondsPerWeek * 52;
778 return getDurationPrettySeconds(numSecs, secondsPerYear, secondsPerDay, secondsPerWeek, resourceBundle, secondsDuration);
779 }
780
781
782
783
784
785
786
787
788
789 public static String getDurationPrettySeconds(long numSecs, long secondsPerDay, long secondsPerWeek, ResourceBundle resourceBundle)
790 {
791 return getDurationPrettySeconds(numSecs, secondsPerDay, secondsPerWeek, resourceBundle, false);
792 }
793
794
795
796
797 private static String getDurationPrettySeconds(long numSecs, long secondsPerYear, long secondsPerDay, long secondsPerWeek, ResourceBundle resourceBundle, boolean secondResolution)
798 {
799 if (numSecs == 0)
800 {
801 if (secondResolution)
802 {
803 return "0 " + getText(resourceBundle, "core.dateutils.seconds");
804 }
805 else
806 {
807 return "0 " + getText(resourceBundle, "core.dateutils.minutes");
808 }
809 }
810
811 StringBuilder result = new StringBuilder();
812
813 if (numSecs >= secondsPerYear)
814 {
815 long years = numSecs / secondsPerYear;
816 result.append(years).append(' ');
817
818 if (years > 1)
819 {
820 result.append(getText(resourceBundle, "core.dateutils.years"));
821 }
822 else
823 {
824 result.append(getText(resourceBundle, "core.dateutils.year"));
825 }
826
827 result.append(", ");
828 numSecs = numSecs % secondsPerYear;
829 }
830
831 if (numSecs >= secondsPerWeek)
832 {
833 long weeks = numSecs / secondsPerWeek;
834 result.append(weeks).append(' ');
835
836 if (weeks > 1)
837 {
838 result.append(getText(resourceBundle, "core.dateutils.weeks"));
839 }
840 else
841 {
842 result.append(getText(resourceBundle, "core.dateutils.week"));
843 }
844
845 result.append(", ");
846 numSecs = numSecs % secondsPerWeek;
847 }
848
849 if (numSecs >= secondsPerDay)
850 {
851 long days = numSecs / secondsPerDay;
852 result.append(days).append(' ');
853
854 if (days > 1)
855 {
856 result.append(getText(resourceBundle, "core.dateutils.days"));
857 }
858 else
859 {
860 result.append(getText(resourceBundle, "core.dateutils.day"));
861 }
862
863 result.append(", ");
864 numSecs = numSecs % secondsPerDay;
865 }
866
867 if (numSecs >= Duration.HOUR.getSeconds())
868 {
869 long hours = numSecs / Duration.HOUR.getSeconds();
870 result.append(hours).append(' ');
871
872 if (hours > 1)
873 {
874 result.append(getText(resourceBundle, "core.dateutils.hours"));
875 }
876 else
877 {
878 result.append(getText(resourceBundle, "core.dateutils.hour"));
879 }
880
881 result.append(", ");
882 numSecs = numSecs % Duration.HOUR.getSeconds();
883 }
884
885 if (numSecs >= Duration.MINUTE.getSeconds())
886 {
887 long minute = numSecs / Duration.MINUTE.getSeconds();
888 result.append(minute).append(' ');
889
890
891 if (minute > 1)
892 {
893 result.append(getText(resourceBundle, "core.dateutils.minutes"));
894 }
895 else
896 {
897 result.append(getText(resourceBundle, "core.dateutils.minute"));
898 }
899
900 result.append(", ");
901
902
903 if (secondResolution)
904 {
905 numSecs = numSecs % Duration.MINUTE.getSeconds();
906 }
907 }
908
909 if (numSecs >= 1 && numSecs < Duration.MINUTE.getSeconds())
910 {
911 result.append(numSecs).append(' ');
912
913
914 if (numSecs > 1)
915 {
916 result.append(getText(resourceBundle, "core.dateutils.seconds"));
917 }
918 else
919 {
920 result.append(getText(resourceBundle, "core.dateutils.second"));
921 }
922
923 result.append(", ");
924 }
925
926 if (result.length() > 2)
927 {
928 return result.substring(0, result.length() - 2);
929 }
930 else
931 {
932 return result.toString();
933 }
934 }
935
936
937
938
939
940 public String formatDurationPretty(long l)
941 {
942 return DateUtils.getDurationPretty(l, resourceBundle);
943 }
944
945
946
947
948
949 public String formatDurationPretty(String seconds)
950 {
951 return DateUtils.getDurationPretty(Long.parseLong(seconds), resourceBundle);
952 }
953
954
955
956
957
958
959
960
961
962 public String formatDurationString(long l)
963 {
964 return DateUtils.getDurationPretty(l, resourceBundle);
965 }
966
967 private static String getText(ResourceBundle resourceBundle, String key)
968 {
969 try
970 {
971 return resourceBundle.getString(key);
972 }
973 catch (MissingResourceException e)
974 {
975 log.error(e);
976 return "";
977 }
978 }
979
980
981
982
983
984
985
986
987
988
989
990 public static Calendar toEndOfPeriod(Calendar calendar, int period)
991 {
992 boolean zero = false;
993
994 for (int calendarPeriod : CALENDAR_PERIODS)
995 {
996 if (zero)
997 {
998 calendar.set(calendarPeriod, calendar.getMaximum(calendarPeriod));
999 }
1000
1001 if (calendarPeriod == period)
1002 {
1003 zero = true;
1004 }
1005 }
1006
1007 if (!zero)
1008 {
1009 throw new IllegalArgumentException("unknown Calendar period: " + period);
1010 }
1011
1012 return calendar;
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022 public static Calendar toStartOfPeriod(Calendar calendar, int period)
1023 {
1024 boolean zero = false;
1025 for (int calendarPeriod : CALENDAR_PERIODS)
1026 {
1027 if (zero)
1028 {
1029 if (calendarPeriod == Calendar.DAY_OF_MONTH)
1030 {
1031 calendar.set(Calendar.DAY_OF_MONTH, 1);
1032 }
1033 else
1034 {
1035 calendar.set(calendarPeriod, 0);
1036 }
1037 }
1038
1039 if (calendarPeriod == period)
1040 {
1041 zero = true;
1042 }
1043
1044 }
1045
1046 if (!zero)
1047 {
1048 throw new IllegalArgumentException("unknown Calendar period: " + period);
1049 }
1050
1051 return calendar;
1052 }
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062 public static DateRange toDateRange(Calendar date, int period)
1063 {
1064
1065 Calendar cal = (Calendar) date.clone();
1066 toStartOfPeriod(cal, period);
1067 Date startDate = new Date(cal.getTimeInMillis());
1068 cal.add(period, 1);
1069 Date endDate = new Date(cal.getTimeInMillis());
1070
1071 return new DateRange(startDate, endDate);
1072 }
1073
1074 public static Calendar getCalendarDay(int year, int month, int day)
1075 {
1076 return initCalendar(year, month, day, 0, 0, 0, 0);
1077 }
1078
1079 public static Date getDateDay(int year, int month, int day)
1080 {
1081 return getCalendarDay(year, month, day).getTime();
1082 }
1083
1084 public static Date getSqlDateDay(int year, int month, int day)
1085 {
1086 return new java.sql.Date(getCalendarDay(year, month, day).getTimeInMillis());
1087 }
1088
1089 public static int get24HourTime(final String meridianIndicator, final int hours)
1090 {
1091
1092 if (hours == 12)
1093 {
1094 if (AM.equalsIgnoreCase(meridianIndicator))
1095 {
1096 return 0;
1097 }
1098
1099 if (PM.equalsIgnoreCase(meridianIndicator))
1100 {
1101 return 12;
1102 }
1103 }
1104
1105 final int onceMeridianAdjustment = PM.equalsIgnoreCase(meridianIndicator) ? 12 : 0;
1106 return hours + onceMeridianAdjustment;
1107 }
1108
1109
1110 public static Date tomorrow()
1111 {
1112 Calendar cal = Calendar.getInstance();
1113 cal.add(Calendar.DAY_OF_MONTH, 1);
1114 return cal.getTime();
1115 }
1116
1117 public static Date yesterday()
1118 {
1119 Calendar cal = Calendar.getInstance();
1120 cal.add(Calendar.DAY_OF_MONTH, -1);
1121 return cal.getTime();
1122 }
1123
1124 private static Calendar initCalendar(int year, int month, int day, int hour, int minute, int second, int millis)
1125 {
1126 Calendar calendar = Calendar.getInstance();
1127 calendar.set(year, month, day, hour, minute, second);
1128 calendar.set(Calendar.MILLISECOND, millis);
1129 return calendar;
1130 }
1131 }