View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.util;
20  
21  import java.sql.Timestamp;
22  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  
26  import org.kuali.kfs.sys.KFSConstants;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  /**
30   * Utility methods for comparing dates
31   *
32   *
33   */
34  public class KfsDateUtils {
35      /**
36       * Adds null-safety to commons.DateUtils isSameDay method.
37       *
38       * @return true if both dates are null or represent the same day
39       */
40      public static boolean isSameDay(Date date1, Date date2) {
41          boolean same = false;
42  
43          if ((date1 == null) && (date2 == null)) {
44              same = true;
45          }
46          else if ((date1 != null) && (date2 != null)) {
47              return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
48          }
49          else {
50              same = false;
51          }
52  
53          return same;
54      }
55  
56      /**
57       * Adds null-safety to commons.DateUtils isSameDay method.
58       *
59       * @return true if both calendars are null or represent the same day
60       */
61      public static boolean isSameDay(Calendar cal1, Calendar cal2) {
62          boolean same = false;
63  
64          if ((cal1 == null) && (cal2 == null)) {
65              same = true;
66          }
67          else if ((cal1 != null) && (cal2 != null)) {
68              return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2);
69          }
70          else {
71              same = false;
72          }
73  
74          return same;
75      }
76  
77      /**
78       * Converts the given java.util.Date into an equivalent java.sql.Date
79       *
80       * @param date
81       * @return java.sql.Date constructed from the given java.util.Date
82       */
83      public static java.sql.Date convertToSqlDate(java.util.Date date) {
84          return new java.sql.Date(date.getTime());
85      }
86  
87  
88      /**
89       * Convert the given java.sql.date into a java.sql.date of which all the time fields are set to 0.
90       *
91       * @param date
92       * @return
93       */
94      public static java.sql.Date clearTimeFields(java.sql.Date date) {
95          Calendar timelessCal = new GregorianCalendar();
96          timelessCal.setTime(date);
97          timelessCal.set(Calendar.HOUR_OF_DAY, 0);
98          timelessCal.set(Calendar.MINUTE, 0);
99          timelessCal.set(Calendar.SECOND, 0);
100         timelessCal.set(Calendar.MILLISECOND, 0);
101 
102         return new java.sql.Date(timelessCal.getTimeInMillis());
103     }
104 
105 
106     /**
107      * Convert the given java.util.date into a java.util.date of which all the time fields are set to 0.
108      *
109      * @param date
110      * @return
111      */
112     public static java.util.Date clearTimeFields(java.util.Date date) {
113         Calendar timelessCal = new GregorianCalendar();
114         timelessCal.setTime(date);
115         timelessCal.set(Calendar.HOUR_OF_DAY, 0);
116         timelessCal.set(Calendar.MINUTE, 0);
117         timelessCal.set(Calendar.SECOND, 0);
118         timelessCal.set(Calendar.MILLISECOND, 0);
119 
120         return new java.util.Date(timelessCal.getTimeInMillis());
121     }
122 
123     /**
124      * @param startDateTime
125      * @param endDateTime
126      * @return the difference in days between the second timestamp and first
127      */
128     public static double getDifferenceInDays(Timestamp startDateTime, Timestamp endDateTime) {
129         int difference = 0;
130 
131         Calendar startCalendar = Calendar.getInstance();
132         startCalendar.setTime(startDateTime);
133 
134         Calendar endCalendar = Calendar.getInstance();
135         endCalendar.setTime(endDateTime);
136 
137         // First, get difference in whole days
138         Calendar startCompare = Calendar.getInstance();
139         startCompare.setTime(startDateTime);
140         startCompare.set(Calendar.HOUR_OF_DAY, 0);
141         startCompare.set(Calendar.MINUTE, 0);
142         startCompare.set(Calendar.SECOND, 0);
143         startCompare.set(Calendar.MILLISECOND, 0);
144 
145         Calendar endCompare = Calendar.getInstance();
146         endCompare.setTime(endDateTime);
147         endCompare.set(Calendar.HOUR_OF_DAY, 0);
148         endCompare.set(Calendar.MINUTE, 0);
149         endCompare.set(Calendar.SECOND, 0);
150         endCompare.set(Calendar.MILLISECOND, 0);
151 
152         return (endCompare.getTimeInMillis() - startCompare.getTimeInMillis()) / ((double)KFSConstants.MILLSECONDS_PER_DAY);
153     }
154 
155     /**
156      * @param startDateTime
157      * @param endDateTime
158      * @return the difference in hours between the second timestamp and first
159      */
160     public static double getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime) {
161         int difference = 0;
162 
163         Calendar startCalendar = Calendar.getInstance();
164         startCalendar.setTime(startDateTime);
165 
166         Calendar endCalendar = Calendar.getInstance();
167         endCalendar.setTime(endDateTime);
168 
169         // First, get difference in whole days
170         Calendar startCompare = Calendar.getInstance();
171         startCompare.setTime(startDateTime);
172         startCompare.set(Calendar.HOUR_OF_DAY, 0);
173         startCompare.set(Calendar.MINUTE, 0);
174 
175         Calendar endCompare = Calendar.getInstance();
176         endCompare.setTime(endDateTime);
177         endCompare.set(Calendar.HOUR_OF_DAY, 0);
178         endCompare.set(Calendar.MINUTE, 0);
179 
180         return (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (60.0000 * 60.0000 * 1000.0000);
181     }
182 
183     /**
184      *
185      * This method is a utility method to create a new java.sql.Date in one line.
186      *
187      * @param year
188      * @param month
189      * @param day
190      *
191      * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
192      *         millisecond
193      *
194      */
195     public static java.sql.Date newDate(Integer year, Integer month, Integer day) {
196 
197         // test for null arguments
198         if (year == null) {
199             throw new IllegalArgumentException("Argument 'year' passed in was null.");
200         }
201         if (month == null) {
202             throw new IllegalArgumentException("Argument 'month' passed in was null.");
203         }
204         if (day == null) {
205             throw new IllegalArgumentException("Argument 'day' passed in was null.");
206         }
207 
208         Calendar calendar = Calendar.getInstance();
209         calendar.set(Calendar.YEAR, year);
210         calendar.set(Calendar.MONTH, month);
211         calendar.set(Calendar.DAY_OF_MONTH, day);
212         calendar.clear(Calendar.HOUR_OF_DAY);
213         calendar.clear(Calendar.MINUTE);
214         calendar.clear(Calendar.SECOND);
215         calendar.clear(Calendar.MILLISECOND);
216 
217         return new java.sql.Date(calendar.getTimeInMillis());
218     }
219 
220     /**
221      *
222      * This method is a utility method to create a new java.sql.Date in one line.
223      *
224      * @param year
225      * @param month
226      * @param day
227      * @param hour
228      * @param minute
229      * @param second
230      *
231      * @return a populated java.sql.Date with the year, month, hour, minute, and second populated, with no value for millisecond.
232      *
233      */
234     public static java.sql.Date newDate(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) {
235 
236         // test for null arguments
237         if (year == null) {
238             throw new IllegalArgumentException("Argument 'year' passed in was null.");
239         }
240         if (month == null) {
241             throw new IllegalArgumentException("Argument 'month' passed in was null.");
242         }
243         if (day == null) {
244             throw new IllegalArgumentException("Argument 'day' passed in was null.");
245         }
246         if (hour == null) {
247             throw new IllegalArgumentException("Argument 'hour' passed in was null.");
248         }
249         if (minute == null) {
250             throw new IllegalArgumentException("Argument 'minute' passed in was null.");
251         }
252         if (second == null) {
253             throw new IllegalArgumentException("Argument 'second' passed in was null.");
254         }
255 
256         Calendar calendar = Calendar.getInstance();
257         calendar.set(Calendar.YEAR, year);
258         calendar.set(Calendar.MONTH, month);
259         calendar.set(Calendar.DAY_OF_MONTH, day);
260         calendar.set(Calendar.HOUR_OF_DAY, hour);
261         calendar.set(Calendar.MINUTE, minute);
262         calendar.set(Calendar.SECOND, second);
263         calendar.clear(Calendar.MILLISECOND);
264 
265         return new java.sql.Date(calendar.getTimeInMillis());
266     }
267 
268     /**
269      * Determines if the given date d1 is on the same day or an earlier day than the given date d2.
270      * @param d1 a date
271      * @param d2 another date, to compare the first date to
272      * @return true if d1 is earlier or the same day as d2, false otherwise or if either value is null
273      */
274     public static boolean isSameDayOrEarlier(Date d1, Date d2) {
275         if (ObjectUtils.isNull(d1) || ObjectUtils.isNull(d2)) {
276             return false;
277         }
278         if (isSameDay(d1, d2)) {
279             return true;
280         }
281         return d1.compareTo(d2) < 0;
282     }
283 
284     /**
285      * Determines if the given date d1 is on the same day or a later day than the given date d2.
286      * @param d1 a date
287      * @param d2 another date, to compare the first date to
288      * @return true if d1 is later or the same day as d2, false otherwise or if either value is null
289      */
290     public static boolean isSameDayOrLater(Date d1, Date d2) {
291         if (ObjectUtils.isNull(d1) || ObjectUtils.isNull(d2)) {
292             return false; // we can't compare against nulls
293         }
294         if (isSameDay(d1, d2)) {
295             return true;
296         }
297         return d1.compareTo(d2) > 0;
298     }
299 }