1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.sql.Timestamp; |
19 | |
import java.text.DateFormat; |
20 | |
import java.text.ParseException; |
21 | |
import java.text.ParsePosition; |
22 | |
import java.text.SimpleDateFormat; |
23 | |
import java.util.Calendar; |
24 | |
import java.util.Date; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.commons.lang.StringUtils; |
28 | |
import org.apache.commons.lang.time.DurationFormatUtils; |
29 | |
import org.kuali.rice.kns.service.DateTimeService; |
30 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
31 | |
import org.kuali.rice.kns.service.ParameterService; |
32 | |
import org.kuali.rice.kns.util.KNSConstants; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class DateTimeServiceImpl implements DateTimeService { |
40 | |
protected String[] stringToDateFormats; |
41 | |
protected String[] stringToTimestampFormats; |
42 | |
protected String dateToStringFormatForUserInterface; |
43 | |
protected String timestampToStringFormatForUserInterface; |
44 | |
protected String dateToStringFormatForFileName; |
45 | |
protected String timestampToStringFormatForFileName; |
46 | |
|
47 | |
public synchronized void initializeDateTimeService() { |
48 | 0 | ParameterService parameterService = KNSServiceLocator.getParameterService(); |
49 | 0 | if (stringToDateFormats == null) { |
50 | 0 | List<String> dateFormatParams = parameterService.getParameterValues(KNSConstants.KNS_NAMESPACE, |
51 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.STRING_TO_DATE_FORMATS); |
52 | |
|
53 | 0 | stringToDateFormats = new String[dateFormatParams.size()]; |
54 | |
|
55 | 0 | for (int i = 0; i < dateFormatParams.size(); i++) { |
56 | 0 | String dateFormatParam = dateFormatParams.get(i); |
57 | 0 | if (StringUtils.isBlank(dateFormatParam)) { |
58 | 0 | throw new IllegalArgumentException("KR-NS/All/STRING_TO_DATE_FORMATS parameter contains a blank semi-colon delimited substring"); |
59 | |
} |
60 | |
else { |
61 | |
|
62 | 0 | new SimpleDateFormat(dateFormatParam); |
63 | 0 | stringToDateFormats[i] = dateFormatParam; |
64 | |
} |
65 | |
} |
66 | |
} |
67 | |
|
68 | 0 | if (stringToTimestampFormats == null) { |
69 | 0 | List<String> dateFormatParams = parameterService.getParameterValues(KNSConstants.KNS_NAMESPACE, |
70 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.STRING_TO_TIMESTAMP_FORMATS); |
71 | |
|
72 | 0 | stringToTimestampFormats = new String[dateFormatParams.size()]; |
73 | |
|
74 | 0 | for (int i = 0; i < dateFormatParams.size(); i++) { |
75 | 0 | String dateFormatParam = dateFormatParams.get(i); |
76 | 0 | if (StringUtils.isBlank(dateFormatParam)) { |
77 | 0 | throw new IllegalArgumentException("KR-NS/All/STRING_TO_TIMESTAMP_FORMATS parameter contains a blank semi-colon delimited substring"); |
78 | |
} |
79 | |
else { |
80 | |
|
81 | 0 | new SimpleDateFormat(dateFormatParam); |
82 | 0 | stringToTimestampFormats[i] = dateFormatParam; |
83 | |
} |
84 | |
} |
85 | |
} |
86 | |
|
87 | 0 | if (dateToStringFormatForUserInterface == null) { |
88 | 0 | dateToStringFormatForUserInterface = parameterService.getParameterValue(KNSConstants.KNS_NAMESPACE, |
89 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.DATE_TO_STRING_FORMAT_FOR_USER_INTERFACE); |
90 | |
|
91 | 0 | new SimpleDateFormat(dateToStringFormatForUserInterface); |
92 | |
} |
93 | |
|
94 | 0 | if (timestampToStringFormatForUserInterface == null) { |
95 | 0 | timestampToStringFormatForUserInterface = parameterService.getParameterValue(KNSConstants.KNS_NAMESPACE, |
96 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.TIMESTAMP_TO_STRING_FORMAT_FOR_USER_INTERFACE); |
97 | |
|
98 | 0 | new SimpleDateFormat(timestampToStringFormatForUserInterface); |
99 | |
} |
100 | |
|
101 | 0 | if (dateToStringFormatForFileName == null) { |
102 | 0 | dateToStringFormatForFileName = parameterService.getParameterValue(KNSConstants.KNS_NAMESPACE, |
103 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.DATE_TO_STRING_FORMAT_FOR_FILE_NAME); |
104 | |
|
105 | 0 | new SimpleDateFormat(dateToStringFormatForFileName); |
106 | |
} |
107 | |
|
108 | 0 | if (timestampToStringFormatForFileName == null) { |
109 | 0 | timestampToStringFormatForFileName = parameterService.getParameterValue(KNSConstants.KNS_NAMESPACE, |
110 | |
KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.TIMESTAMP_TO_STRING_FORMAT_FOR_FILE_NAME); |
111 | |
|
112 | 0 | new SimpleDateFormat(timestampToStringFormatForFileName); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
public String toDateString(Date date) { |
119 | 0 | return toString(date, dateToStringFormatForUserInterface); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public String toDateTimeString(Date date) { |
126 | 0 | return toString(date, timestampToStringFormatForUserInterface); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public String toString(Date date, String pattern) { |
134 | 0 | DateFormat dateFormat = new SimpleDateFormat(pattern); |
135 | 0 | dateFormat.setLenient(false); |
136 | 0 | return dateFormat.format(date); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public Date getCurrentDate() { |
143 | 0 | Calendar c = Calendar.getInstance(); |
144 | 0 | c.setTime(new Date()); |
145 | 0 | return c.getTime(); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public Timestamp getCurrentTimestamp() { |
152 | 0 | return new java.sql.Timestamp(getCurrentDate().getTime()); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public java.sql.Date getCurrentSqlDate() { |
159 | 0 | return new java.sql.Date(getCurrentDate().getTime()); |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public java.sql.Date getCurrentSqlDateMidnight() { |
166 | |
|
167 | 0 | return java.sql.Date.valueOf(getCurrentSqlDate().toString()); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public Calendar getCurrentCalendar() { |
174 | 0 | return getCalendar(getCurrentDate()); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public Calendar getCalendar(Date date) { |
181 | 0 | if (date == null) { |
182 | 0 | throw new IllegalArgumentException("invalid (null) date"); |
183 | |
} |
184 | |
|
185 | 0 | Calendar currentCalendar = Calendar.getInstance(); |
186 | 0 | currentCalendar.setTime(date); |
187 | |
|
188 | 0 | return currentCalendar; |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public Date convertToDate(String dateString) throws ParseException { |
197 | 0 | return parseAgainstFormatArray(dateString, stringToDateFormats); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public Date convertToDateTime(String dateTimeString) throws ParseException { |
204 | 0 | if (StringUtils.isBlank(dateTimeString)) { |
205 | 0 | throw new IllegalArgumentException("invalid (blank) date/time string"); |
206 | |
} |
207 | 0 | return parseAgainstFormatArray(dateTimeString, stringToTimestampFormats); |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public java.sql.Timestamp convertToSqlTimestamp(String timeString) |
214 | |
throws ParseException { |
215 | 0 | if (!StringUtils.isBlank(timeString)) { |
216 | 0 | return new java.sql.Timestamp(convertToDateTime(timeString).getTime()); |
217 | |
} |
218 | 0 | return null; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public java.sql.Date convertToSqlDate(String dateString) |
225 | |
throws ParseException { |
226 | 0 | if (StringUtils.isBlank(dateString)) { |
227 | 0 | throw new IllegalArgumentException("invalid (blank) timeString"); |
228 | |
} |
229 | 0 | Date date = parseAgainstFormatArray(dateString, stringToDateFormats); |
230 | 0 | return new java.sql.Date(date.getTime()); |
231 | |
} |
232 | |
|
233 | |
protected Date parseAgainstFormatArray(String dateString, String[] formats) throws ParseException { |
234 | 0 | dateString = dateString.trim(); |
235 | 0 | StringBuffer exceptionMessage = new StringBuffer("Date or date/time string '") |
236 | |
.append(dateString) |
237 | |
.append("' could not be converted using any of the accepted formats: "); |
238 | 0 | for (String dateFormatString : formats) { |
239 | |
try { |
240 | 0 | return parse(dateString, dateFormatString); |
241 | 0 | } catch (ParseException e) { |
242 | 0 | exceptionMessage.append(dateFormatString).append( |
243 | |
" (error offset=").append(e.getErrorOffset()).append( |
244 | |
"),"); |
245 | |
} |
246 | |
} |
247 | 0 | throw new ParseException(exceptionMessage.toString().substring(0, |
248 | |
exceptionMessage.length() - 1), 0); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public java.sql.Date convertToSqlDate(Timestamp timestamp) |
256 | |
throws ParseException { |
257 | 0 | return new java.sql.Date(timestamp.getTime()); |
258 | |
} |
259 | |
|
260 | |
public int dateDiff(Date startDate, Date endDate, boolean inclusive) { |
261 | 0 | Calendar startDateCalendar = Calendar.getInstance(); |
262 | 0 | startDateCalendar.setTime(startDate); |
263 | |
|
264 | 0 | Calendar endDateCalendar = Calendar.getInstance(); |
265 | 0 | endDateCalendar.setTime(endDate); |
266 | |
|
267 | 0 | int startDateOffset = -(startDateCalendar.get(Calendar.ZONE_OFFSET) + startDateCalendar |
268 | |
.get(Calendar.DST_OFFSET)) |
269 | |
/ (60 * 1000); |
270 | |
|
271 | 0 | int endDateOffset = -(endDateCalendar.get(Calendar.ZONE_OFFSET) + endDateCalendar |
272 | |
.get(Calendar.DST_OFFSET)) |
273 | |
/ (60 * 1000); |
274 | |
|
275 | 0 | if (startDateOffset > endDateOffset) { |
276 | 0 | startDateCalendar.add(Calendar.MINUTE, endDateOffset |
277 | |
- startDateOffset); |
278 | |
} |
279 | |
|
280 | 0 | if (inclusive) { |
281 | 0 | startDateCalendar.add(Calendar.DATE, -1); |
282 | |
} |
283 | |
|
284 | 0 | int dateDiff = Integer.parseInt(DurationFormatUtils.formatDuration( |
285 | |
endDateCalendar.getTimeInMillis() |
286 | |
- startDateCalendar.getTimeInMillis(), "d", true)); |
287 | |
|
288 | 0 | return dateDiff; |
289 | |
} |
290 | |
|
291 | |
protected Date parse(String dateString, String pattern) throws ParseException { |
292 | 0 | if (!StringUtils.isBlank(dateString)) { |
293 | 0 | DateFormat dateFormat = new SimpleDateFormat(pattern); |
294 | 0 | dateFormat.setLenient(false); |
295 | 0 | ParsePosition parsePosition = new ParsePosition(0); |
296 | 0 | Date testDate = dateFormat.parse(dateString, parsePosition); |
297 | |
|
298 | |
|
299 | 0 | if (testDate == null) { |
300 | 0 | throw new ParseException("The date that you provided is invalid.",parsePosition.getErrorIndex()); |
301 | 0 | } else if (parsePosition.getIndex() != dateString.length()) { |
302 | 0 | throw new ParseException("The date that you provided is invalid.",parsePosition.getIndex()); |
303 | |
} |
304 | |
|
305 | |
|
306 | 0 | Calendar testCalendar = Calendar.getInstance(); |
307 | 0 | testCalendar.setLenient(false); |
308 | 0 | testCalendar.setTime(testDate); |
309 | 0 | if (testCalendar.get(Calendar.YEAR) < 1000 || testCalendar.get(Calendar.YEAR) > 9999) { |
310 | 0 | throw new ParseException("The date that you provided is not between the years 1000 and 9999.",-1); |
311 | |
} |
312 | |
|
313 | 0 | if(testCalendar.get(Calendar.YEAR) == 1970 && !pattern.contains("y".toLowerCase())){ |
314 | 0 | Calendar curCalendar = Calendar.getInstance(); |
315 | 0 | curCalendar.setTime(new java.util.Date()); |
316 | 0 | testCalendar.set(Calendar.YEAR, curCalendar.get(Calendar.YEAR)); |
317 | 0 | testDate = testCalendar.getTime(); |
318 | |
} |
319 | |
|
320 | 0 | return testDate; |
321 | |
} |
322 | 0 | return null; |
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
public String toDateStringForFilename(Date date) { |
329 | 0 | SimpleDateFormat dateFormat = new SimpleDateFormat(dateToStringFormatForFileName); |
330 | 0 | return dateFormat.format(date); |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
public String toDateTimeStringForFilename(Date date) { |
337 | 0 | SimpleDateFormat dateFormat = new SimpleDateFormat(timestampToStringFormatForFileName); |
338 | 0 | return dateFormat.format(date); |
339 | |
} |
340 | |
} |