1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.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 public class KfsDateUtils {
29
30
31
32
33
34 public static boolean isSameDay(Date date1, Date date2) {
35 boolean same = false;
36
37 if ((date1 == null) && (date2 == null)) {
38 same = true;
39 }
40 else if ((date1 != null) && (date2 != null)) {
41 return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
42 }
43 else {
44 same = false;
45 }
46
47 return same;
48 }
49
50
51
52
53
54
55 public static boolean isSameDay(Calendar cal1, Calendar cal2) {
56 boolean same = false;
57
58 if ((cal1 == null) && (cal2 == null)) {
59 same = true;
60 }
61 else if ((cal1 != null) && (cal2 != null)) {
62 return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2);
63 }
64 else {
65 same = false;
66 }
67
68 return same;
69 }
70
71
72
73
74
75
76
77 public static java.sql.Date convertToSqlDate(java.util.Date date) {
78 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 Calendar timelessCal = new GregorianCalendar();
90 timelessCal.setTime(date);
91 timelessCal.set(Calendar.HOUR_OF_DAY, 0);
92 timelessCal.set(Calendar.MINUTE, 0);
93 timelessCal.set(Calendar.SECOND, 0);
94 timelessCal.set(Calendar.MILLISECOND, 0);
95
96 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 Calendar timelessCal = new GregorianCalendar();
108 timelessCal.setTime(date);
109 timelessCal.set(Calendar.HOUR_OF_DAY, 0);
110 timelessCal.set(Calendar.MINUTE, 0);
111 timelessCal.set(Calendar.SECOND, 0);
112 timelessCal.set(Calendar.MILLISECOND, 0);
113
114 return new java.util.Date(timelessCal.getTimeInMillis());
115 }
116
117
118
119
120
121
122 public static double getDifferenceInDays(Timestamp startDateTime, Timestamp endDateTime) {
123 int difference = 0;
124
125 Calendar startCalendar = Calendar.getInstance();
126 startCalendar.setTime(startDateTime);
127
128 Calendar endCalendar = Calendar.getInstance();
129 endCalendar.setTime(endDateTime);
130
131
132 Calendar startCompare = Calendar.getInstance();
133 startCompare.setTime(startDateTime);
134 startCompare.set(Calendar.HOUR_OF_DAY, 0);
135 startCompare.set(Calendar.MINUTE, 0);
136 startCompare.set(Calendar.SECOND, 0);
137 startCompare.set(Calendar.MILLISECOND, 0);
138
139 Calendar endCompare = Calendar.getInstance();
140 endCompare.setTime(endDateTime);
141 endCompare.set(Calendar.HOUR_OF_DAY, 0);
142 endCompare.set(Calendar.MINUTE, 0);
143 endCompare.set(Calendar.SECOND, 0);
144 endCompare.set(Calendar.MILLISECOND, 0);
145
146 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 int difference = 0;
156
157 Calendar startCalendar = Calendar.getInstance();
158 startCalendar.setTime(startDateTime);
159
160 Calendar endCalendar = Calendar.getInstance();
161 endCalendar.setTime(endDateTime);
162
163
164 Calendar startCompare = Calendar.getInstance();
165 startCompare.setTime(startDateTime);
166 startCompare.set(Calendar.HOUR_OF_DAY, 0);
167 startCompare.set(Calendar.MINUTE, 0);
168
169 Calendar endCompare = Calendar.getInstance();
170 endCompare.setTime(endDateTime);
171 endCompare.set(Calendar.HOUR_OF_DAY, 0);
172 endCompare.set(Calendar.MINUTE, 0);
173
174 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 if (year == null) {
193 throw new IllegalArgumentException("Argument 'year' passed in was null.");
194 }
195 if (month == null) {
196 throw new IllegalArgumentException("Argument 'month' passed in was null.");
197 }
198 if (day == null) {
199 throw new IllegalArgumentException("Argument 'day' passed in was null.");
200 }
201
202 Calendar calendar = Calendar.getInstance();
203 calendar.set(Calendar.YEAR, year);
204 calendar.set(Calendar.MONTH, month);
205 calendar.set(Calendar.DAY_OF_MONTH, day);
206 calendar.clear(Calendar.HOUR_OF_DAY);
207 calendar.clear(Calendar.MINUTE);
208 calendar.clear(Calendar.SECOND);
209 calendar.clear(Calendar.MILLISECOND);
210
211 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 if (year == null) {
232 throw new IllegalArgumentException("Argument 'year' passed in was null.");
233 }
234 if (month == null) {
235 throw new IllegalArgumentException("Argument 'month' passed in was null.");
236 }
237 if (day == null) {
238 throw new IllegalArgumentException("Argument 'day' passed in was null.");
239 }
240 if (hour == null) {
241 throw new IllegalArgumentException("Argument 'hour' passed in was null.");
242 }
243 if (minute == null) {
244 throw new IllegalArgumentException("Argument 'minute' passed in was null.");
245 }
246 if (second == null) {
247 throw new IllegalArgumentException("Argument 'second' passed in was null.");
248 }
249
250 Calendar calendar = Calendar.getInstance();
251 calendar.set(Calendar.YEAR, year);
252 calendar.set(Calendar.MONTH, month);
253 calendar.set(Calendar.DAY_OF_MONTH, day);
254 calendar.set(Calendar.HOUR_OF_DAY, hour);
255 calendar.set(Calendar.MINUTE, minute);
256 calendar.set(Calendar.SECOND, second);
257 calendar.clear(Calendar.MILLISECOND);
258
259 return new java.sql.Date(calendar.getTimeInMillis());
260 }
261 }