1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.r2.common.util;
18
19 import org.kuali.student.r2.common.dto.TimeOfDayInfo;
20 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
21 import org.kuali.student.r2.common.infc.TimeOfDay;
22
23 import java.util.List;
24
25
26
27
28
29
30 public class TimeOfDayHelper {
31 public static final long MILLIS_PER_MINUTE = 60000L;
32 public static final int MINUTES_PER_HOUR = 60;
33
34 public static TimeOfDay createTimeOfDay(int normalHours, int minutes, TimeOfDayAmPmEnum amOrPm) throws InvalidParameterException {
35 if (normalHours < 1 || normalHours > 12 || minutes < 0 || minutes > 59) {
36 throw new InvalidParameterException("Invalid values for hours or minutes");
37 }
38
39 int milHours = normalHours;
40 if (amOrPm == TimeOfDayAmPmEnum.AM) {
41 if (normalHours == 12) {
42 milHours = 0;
43 }
44 } else {
45
46 if (normalHours != 12) {
47 milHours += 12;
48 }
49 }
50 return createTimeOfDayInMilitary(milHours, minutes);
51 }
52
53 public static TimeOfDay createTimeOfDayInMilitary(int milHours, int minutes) throws InvalidParameterException {
54 if (milHours < 0 || milHours > 23 || minutes < 0 || minutes > 59) {
55 throw new InvalidParameterException("Invalid values for military hours or minutes");
56 }
57
58
59 long millis = (milHours * MINUTES_PER_HOUR + minutes) * MILLIS_PER_MINUTE;
60 TimeOfDayInfo info = new TimeOfDayInfo();
61 info.setMilliSeconds(millis);
62 return info;
63 }
64
65 public static String formatTimeOfDay(TimeOfDay timeOfDay) throws InvalidParameterException {
66 return formatTimeOfDay(timeOfDay, null);
67 }
68
69 public static String formatTimeOfDay(TimeOfDay timeOfDay, List<TimeOfDayFormattingEnum> options) throws InvalidParameterException {
70 int hours = 0;
71 boolean isMilitary = false;
72 boolean skip = false;
73 if (options == null) {
74 skip = true;
75 }
76 if (!skip && options.contains(TimeOfDayFormattingEnum.USE_MILITARY_TIME)) {
77 isMilitary = true;
78 hours = getHoursInMilitaryTime(timeOfDay);
79 } else {
80 hours = getHours(timeOfDay);
81 }
82 String hoursStr = hours + "";
83 if (!skip && options.contains(TimeOfDayFormattingEnum.USE_TWO_DIGITS_FOR_HOURS) && hoursStr.length() < 2) {
84 hoursStr = "0" + hoursStr;
85 }
86 int minutes = getMinutes(timeOfDay);
87 String minutesStr = minutes + "";
88 if (minutesStr.length() < 2) {
89 minutesStr = "0" + minutesStr;
90 }
91 boolean isAM = isAM(timeOfDay);
92 String amOrPm = "";
93 if (!isMilitary) {
94 if (!skip && options.contains(TimeOfDayFormattingEnum.USE_ALL_CAPS_AM_PM)) {
95 amOrPm = isAM ? "AM" : "PM";
96 } else if (!skip && options.contains(TimeOfDayFormattingEnum.USE_ALL_CAPS_AM_PM)) {
97 amOrPm = isAM ? "Am" : "Pm";
98 } else {
99 amOrPm = isAM ? "am" : "pm";
100 }
101 if (!skip && options.contains(TimeOfDayFormattingEnum.USE_ONLY_FIRST_LETTER_AM_PM)) {
102
103 amOrPm = amOrPm.substring(0, 1);
104 }
105 }
106 String formattedTime = hoursStr + ":" + minutesStr;
107 if (!isMilitary) {
108 formattedTime += " " + amOrPm;
109 }
110 return formattedTime;
111 }
112
113
114
115
116
117
118
119 public static TimeOfDayInfo roundToNearestMinute(TimeOfDay input) {
120 if (input == null) {
121 return null;
122 }
123 long newMillis = (input.getMilliSeconds() / MILLIS_PER_MINUTE) * MILLIS_PER_MINUTE;
124 TimeOfDayInfo newInfo = new TimeOfDayInfo();
125 newInfo.setMilliSeconds(newMillis);
126 return newInfo;
127 }
128
129
130
131
132
133
134
135
136 public static int getHours(TimeOfDay timeOfDay) throws InvalidParameterException {
137 int militaryHours = getHoursInMilitaryTime(timeOfDay);
138 int normalHours = 0;
139 if (militaryHours == 0) {
140 normalHours = 12;
141 } else if (militaryHours <= 12) {
142 normalHours = militaryHours;
143 } else {
144 normalHours = militaryHours - 12;
145 }
146 return normalHours;
147 }
148
149
150
151
152
153
154
155 public static int getHoursInMilitaryTime(TimeOfDay timeOfDay) throws InvalidParameterException {
156 validateTimeOfDayInfo(timeOfDay);
157 long timeInMillis = timeOfDay.getMilliSeconds();
158 long hours = timeInMillis / (MILLIS_PER_MINUTE * MINUTES_PER_HOUR);
159 if (hours < 0 || hours > 23) {
160 throw new InvalidParameterException("Contains an invalid time of day");
161 }
162 return (int) hours;
163 }
164
165
166
167
168
169
170
171
172 public static int getMinutes(TimeOfDay timeOfDay) throws InvalidParameterException {
173 validateTimeOfDayInfo(timeOfDay);
174 long timeInMillis = timeOfDay.getMilliSeconds();
175 int minutesInDay = (int) (timeInMillis / MILLIS_PER_MINUTE);
176 int minuteOffset = minutesInDay % MINUTES_PER_HOUR;
177 return minuteOffset;
178 }
179
180
181
182
183
184
185
186 public static boolean isAM(TimeOfDay timeOfDay) throws InvalidParameterException {
187 validateTimeOfDayInfo(timeOfDay);
188 int milHours = getHoursInMilitaryTime(timeOfDay);
189 return milHours < 12;
190 }
191
192
193
194
195
196
197
198 public static boolean isPM(TimeOfDay timeOfDay) throws InvalidParameterException {
199 return !isAM(timeOfDay);
200 }
201
202
203
204
205
206
207 public static void validateTimeOfDayInfo(TimeOfDay timeOfDay) throws InvalidParameterException {
208 if (timeOfDay == null) {
209 throw new InvalidParameterException("getMilliSeconds() is null");
210 }
211 Long timeInMillis = timeOfDay.getMilliSeconds();
212 if (timeInMillis < 0) {
213 throw new InvalidParameterException("getMilliSeconds() is negative");
214 }
215 Long threshold = 24 * MINUTES_PER_HOUR * MILLIS_PER_MINUTE;
216 threshold -= MILLIS_PER_MINUTE;
217 if (timeInMillis > threshold) {
218 throw new InvalidParameterException("getMilliSeconds() is larger than 11:59 PM");
219 }
220 }
221 }