View Javadoc
1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Utility methods for comparing dates
25   *
26   *
27   */
28  public class KfsDateUtils {
29      /**
30       * Adds null-safety to commons.DateUtils isSameDay method.
31       *
32       * @return true if both dates are null or represent the same day
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       * Adds null-safety to commons.DateUtils isSameDay method.
52       *
53       * @return true if both calendars are null or represent the same day
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       * Converts the given java.util.Date into an equivalent java.sql.Date
73       *
74       * @param date
75       * @return java.sql.Date constructed from the given java.util.Date
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       * Convert the given java.sql.date into a java.sql.date of which all the time fields are set to 0.
84       *
85       * @param date
86       * @return
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      * Convert the given java.util.date into a java.util.date of which all the time fields are set to 0.
102      *
103      * @param date
104      * @return
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      * @param startDateTime
119      * @param endDateTime
120      * @return the difference in days between the second timestamp and first
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         // First, get difference in whole days
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      * @param startDateTime
151      * @param endDateTime
152      * @return the difference in hours between the second timestamp and first
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         // First, get difference in whole days
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      * This method is a utility method to create a new java.sql.Date in one line.
180      *
181      * @param year
182      * @param month
183      * @param day
184      *
185      * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
186      *         millisecond
187      *
188      */
189     public static java.sql.Date newDate(Integer year, Integer month, Integer day) {
190 
191         // test for null arguments
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      * This method is a utility method to create a new java.sql.Date in one line.
217      *
218      * @param year
219      * @param month
220      * @param day
221      * @param hour
222      * @param minute
223      * @param second
224      *
225      * @return a populated java.sql.Date with the year, month, hour, minute, and second populated, with no value for millisecond.
226      *
227      */
228     public static java.sql.Date newDate(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second) {
229 
230         // test for null arguments
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 }