1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.util; |
17 | |
|
18 | |
import java.sql.Timestamp; |
19 | |
import java.util.Calendar; |
20 | |
import java.util.Date; |
21 | |
import java.util.GregorianCalendar; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class DateUtils extends org.apache.commons.lang.time.DateUtils { |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public static boolean isSameDay(Date date1, Date date2) { |
35 | 0 | boolean same = false; |
36 | |
|
37 | 0 | if ((date1 == null) && (date2 == null)) { |
38 | 0 | same = true; |
39 | |
} |
40 | 0 | else if ((date1 != null) && (date2 != null)) { |
41 | 0 | return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2); |
42 | |
} |
43 | |
else { |
44 | 0 | same = false; |
45 | |
} |
46 | |
|
47 | 0 | return same; |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public static boolean isSameDay(Calendar cal1, Calendar cal2) { |
56 | 0 | boolean same = false; |
57 | |
|
58 | 0 | if ((cal1 == null) && (cal2 == null)) { |
59 | 0 | same = true; |
60 | |
} |
61 | 0 | else if ((cal1 != null) && (cal2 != null)) { |
62 | 0 | return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2); |
63 | |
} |
64 | |
else { |
65 | 0 | same = false; |
66 | |
} |
67 | |
|
68 | 0 | return same; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public static java.sql.Date convertToSqlDate(java.util.Date date) { |
78 | 0 | return new java.sql.Date(date.getTime()); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static java.sql.Date clearTimeFields(java.sql.Date date) { |
89 | 0 | Calendar timelessCal = new GregorianCalendar(); |
90 | 0 | timelessCal.setTime(date); |
91 | 0 | timelessCal.set(Calendar.HOUR_OF_DAY, 0); |
92 | 0 | timelessCal.set(Calendar.MINUTE, 0); |
93 | 0 | timelessCal.set(Calendar.SECOND, 0); |
94 | 0 | timelessCal.set(Calendar.MILLISECOND, 0); |
95 | |
|
96 | 0 | return new java.sql.Date(timelessCal.getTimeInMillis()); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public static java.util.Date clearTimeFields(java.util.Date date) { |
107 | 0 | Calendar timelessCal = new GregorianCalendar(); |
108 | 0 | timelessCal.setTime(date); |
109 | 0 | timelessCal.set(Calendar.HOUR_OF_DAY, 0); |
110 | 0 | timelessCal.set(Calendar.MINUTE, 0); |
111 | 0 | timelessCal.set(Calendar.SECOND, 0); |
112 | 0 | timelessCal.set(Calendar.MILLISECOND, 0); |
113 | |
|
114 | 0 | return new java.util.Date(timelessCal.getTimeInMillis()); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public static double getDifferenceInDays(Timestamp startDateTime, Timestamp endDateTime) { |
123 | 0 | int difference = 0; |
124 | |
|
125 | 0 | Calendar startCalendar = Calendar.getInstance(); |
126 | 0 | startCalendar.setTime(startDateTime); |
127 | |
|
128 | 0 | Calendar endCalendar = Calendar.getInstance(); |
129 | 0 | endCalendar.setTime(endDateTime); |
130 | |
|
131 | |
|
132 | 0 | Calendar startCompare = Calendar.getInstance(); |
133 | 0 | startCompare.setTime(startDateTime); |
134 | 0 | startCompare.set(Calendar.HOUR_OF_DAY, 0); |
135 | 0 | startCompare.set(Calendar.MINUTE, 0); |
136 | 0 | startCompare.set(Calendar.SECOND, 0); |
137 | 0 | startCompare.set(Calendar.MILLISECOND, 0); |
138 | |
|
139 | 0 | Calendar endCompare = Calendar.getInstance(); |
140 | 0 | endCompare.setTime(endDateTime); |
141 | 0 | endCompare.set(Calendar.HOUR_OF_DAY, 0); |
142 | 0 | endCompare.set(Calendar.MINUTE, 0); |
143 | 0 | endCompare.set(Calendar.SECOND, 0); |
144 | 0 | endCompare.set(Calendar.MILLISECOND, 0); |
145 | |
|
146 | 0 | return (endCompare.getTimeInMillis() - startCompare.getTimeInMillis()) / (24 * 60 * 60 * 1000); |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public static double getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime) { |
155 | 0 | int difference = 0; |
156 | |
|
157 | 0 | Calendar startCalendar = Calendar.getInstance(); |
158 | 0 | startCalendar.setTime(startDateTime); |
159 | |
|
160 | 0 | Calendar endCalendar = Calendar.getInstance(); |
161 | 0 | endCalendar.setTime(endDateTime); |
162 | |
|
163 | |
|
164 | 0 | Calendar startCompare = Calendar.getInstance(); |
165 | 0 | startCompare.setTime(startDateTime); |
166 | 0 | startCompare.set(Calendar.HOUR_OF_DAY, 0); |
167 | 0 | startCompare.set(Calendar.MINUTE, 0); |
168 | |
|
169 | 0 | Calendar endCompare = Calendar.getInstance(); |
170 | 0 | endCompare.setTime(endDateTime); |
171 | 0 | endCompare.set(Calendar.HOUR_OF_DAY, 0); |
172 | 0 | endCompare.set(Calendar.MINUTE, 0); |
173 | |
|
174 | 0 | return (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (60.0000 * 60.0000 * 1000.0000); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public static java.sql.Date newDate(Integer year, Integer month, Integer day) { |
190 | |
|
191 | |
|
192 | 0 | if (year == null) { |
193 | 0 | throw new IllegalArgumentException("Argument 'year' passed in was null."); |
194 | |
} |
195 | 0 | if (month == null) { |
196 | 0 | throw new IllegalArgumentException("Argument 'month' passed in was null."); |
197 | |
} |
198 | 0 | if (day == null) { |
199 | 0 | throw new IllegalArgumentException("Argument 'day' passed in was null."); |
200 | |
} |
201 | |
|
202 | 0 | Calendar calendar = Calendar.getInstance(); |
203 | 0 | calendar.set(Calendar.YEAR, year); |
204 | 0 | calendar.set(Calendar.MONTH, month); |
205 | 0 | calendar.set(Calendar.DAY_OF_MONTH, day); |
206 | 0 | calendar.clear(Calendar.HOUR_OF_DAY); |
207 | 0 | calendar.clear(Calendar.MINUTE); |
208 | 0 | calendar.clear(Calendar.SECOND); |
209 | 0 | calendar.clear(Calendar.MILLISECOND); |
210 | |
|
211 | 0 | return new java.sql.Date(calendar.getTimeInMillis()); |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public static java.sql.Date newDate(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) { |
229 | |
|
230 | |
|
231 | 0 | if (year == null) { |
232 | 0 | throw new IllegalArgumentException("Argument 'year' passed in was null."); |
233 | |
} |
234 | 0 | if (month == null) { |
235 | 0 | throw new IllegalArgumentException("Argument 'month' passed in was null."); |
236 | |
} |
237 | 0 | if (day == null) { |
238 | 0 | throw new IllegalArgumentException("Argument 'day' passed in was null."); |
239 | |
} |
240 | 0 | if (hour == null) { |
241 | 0 | throw new IllegalArgumentException("Argument 'hour' passed in was null."); |
242 | |
} |
243 | 0 | if (minute == null) { |
244 | 0 | throw new IllegalArgumentException("Argument 'minute' passed in was null."); |
245 | |
} |
246 | 0 | if (second == null) { |
247 | 0 | throw new IllegalArgumentException("Argument 'second' passed in was null."); |
248 | |
} |
249 | |
|
250 | 0 | Calendar calendar = Calendar.getInstance(); |
251 | 0 | calendar.set(Calendar.YEAR, year); |
252 | 0 | calendar.set(Calendar.MONTH, month); |
253 | 0 | calendar.set(Calendar.DAY_OF_MONTH, day); |
254 | 0 | calendar.set(Calendar.HOUR_OF_DAY, hour); |
255 | 0 | calendar.set(Calendar.MINUTE, minute); |
256 | 0 | calendar.set(Calendar.SECOND, second); |
257 | 0 | calendar.clear(Calendar.MILLISECOND); |
258 | |
|
259 | 0 | return new java.sql.Date(calendar.getTimeInMillis()); |
260 | |
} |
261 | |
} |