001: /*
002: * Copyright 2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util;
017:
018: import java.sql.Timestamp;
019: import java.util.Calendar;
020: import java.util.Date;
021: import java.util.GregorianCalendar;
022:
023: /**
024: * Utility methods for comparing dates
025: *
026: *
027: */
028: public class DateUtils extends org.apache.commons.lang.time.DateUtils {
029: /**
030: * Adds null-safety to commons.DateUtils isSameDay method.
031: *
032: * @return true if both dates are null or represent the same day
033: */
034: public static boolean isSameDay(Date date1, Date date2) {
035: boolean same = false;
036:
037: if ((date1 == null) && (date2 == null)) {
038: same = true;
039: } else if ((date1 != null) && (date2 != null)) {
040: return org.apache.commons.lang.time.DateUtils.isSameDay(
041: date1, date2);
042: } else {
043: same = false;
044: }
045:
046: return same;
047: }
048:
049: /**
050: * Adds null-safety to commons.DateUtils isSameDay method.
051: *
052: * @return true if both calendars are null or represent the same day
053: */
054: public static boolean isSameDay(Calendar cal1, Calendar cal2) {
055: boolean same = false;
056:
057: if ((cal1 == null) && (cal2 == null)) {
058: same = true;
059: } else if ((cal1 != null) && (cal2 != null)) {
060: return org.apache.commons.lang.time.DateUtils.isSameDay(
061: cal1, cal2);
062: } else {
063: same = false;
064: }
065:
066: return same;
067: }
068:
069: /**
070: * Converts the given java.util.Date into an equivalent java.sql.Date
071: *
072: * @param date
073: * @return java.sql.Date constructed from the given java.util.Date
074: */
075: public static java.sql.Date convertToSqlDate(java.util.Date date) {
076: return new java.sql.Date(date.getTime());
077: }
078:
079: /**
080: * Convert the given java.sql.date into a java.sql.date of which all the time fields are set to 0.
081: *
082: * @param date
083: * @return
084: */
085: public static java.sql.Date clearTimeFields(java.sql.Date date) {
086: Calendar timelessCal = new GregorianCalendar();
087: timelessCal.setTime(date);
088: timelessCal.set(Calendar.HOUR_OF_DAY, 0);
089: timelessCal.set(Calendar.MINUTE, 0);
090: timelessCal.set(Calendar.SECOND, 0);
091: timelessCal.set(Calendar.MILLISECOND, 0);
092:
093: return new java.sql.Date(timelessCal.getTimeInMillis());
094: }
095:
096: /**
097: * Convert the given java.util.date into a java.util.date of which all the time fields are set to 0.
098: *
099: * @param date
100: * @return
101: */
102: public static java.util.Date clearTimeFields(java.util.Date date) {
103: Calendar timelessCal = new GregorianCalendar();
104: timelessCal.setTime(date);
105: timelessCal.set(Calendar.HOUR_OF_DAY, 0);
106: timelessCal.set(Calendar.MINUTE, 0);
107: timelessCal.set(Calendar.SECOND, 0);
108: timelessCal.set(Calendar.MILLISECOND, 0);
109:
110: return new java.util.Date(timelessCal.getTimeInMillis());
111: }
112:
113: /**
114: * @param startDateTime
115: * @param endDateTime
116: * @return the difference in days between the second timestamp and first
117: */
118: public static double getDifferenceInDays(Timestamp startDateTime,
119: Timestamp endDateTime) {
120: int difference = 0;
121:
122: Calendar startCalendar = Calendar.getInstance();
123: startCalendar.setTime(startDateTime);
124:
125: Calendar endCalendar = Calendar.getInstance();
126: endCalendar.setTime(endDateTime);
127:
128: // First, get difference in whole days
129: Calendar startCompare = Calendar.getInstance();
130: startCompare.setTime(startDateTime);
131: startCompare.set(Calendar.HOUR_OF_DAY, 0);
132: startCompare.set(Calendar.MINUTE, 0);
133: startCompare.set(Calendar.SECOND, 0);
134: startCompare.set(Calendar.MILLISECOND, 0);
135:
136: Calendar endCompare = Calendar.getInstance();
137: endCompare.setTime(endDateTime);
138: endCompare.set(Calendar.HOUR_OF_DAY, 0);
139: endCompare.set(Calendar.MINUTE, 0);
140: endCompare.set(Calendar.SECOND, 0);
141: endCompare.set(Calendar.MILLISECOND, 0);
142:
143: return (endCompare.getTimeInMillis() - startCompare
144: .getTimeInMillis())
145: / (24 * 60 * 60 * 1000);
146: }
147:
148: /**
149: * @param startDateTime
150: * @param endDateTime
151: * @return the difference in hours between the second timestamp and first
152: */
153: public static double getDifferenceInHours(Timestamp startDateTime,
154: 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
175: .getTimeInMillis())
176: / (60.0000 * 60.0000 * 1000.0000);
177: }
178:
179: /**
180: *
181: * This method is a utility method to create a new java.sql.Date in one line.
182: *
183: * @param year
184: * @param month
185: * @param day
186: *
187: * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
188: * millisecond
189: *
190: */
191: public static java.sql.Date newDate(Integer year, Integer month,
192: Integer day) {
193:
194: // test for null arguments
195: if (year == null) {
196: throw new IllegalArgumentException(
197: "Argument 'year' passed in was null.");
198: }
199: if (month == null) {
200: throw new IllegalArgumentException(
201: "Argument 'month' passed in was null.");
202: }
203: if (day == null) {
204: throw new IllegalArgumentException(
205: "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,
235: Integer day, Integer hour, Integer minute, Integer second) {
236:
237: // test for null arguments
238: if (year == null) {
239: throw new IllegalArgumentException(
240: "Argument 'year' passed in was null.");
241: }
242: if (month == null) {
243: throw new IllegalArgumentException(
244: "Argument 'month' passed in was null.");
245: }
246: if (day == null) {
247: throw new IllegalArgumentException(
248: "Argument 'day' passed in was null.");
249: }
250: if (hour == null) {
251: throw new IllegalArgumentException(
252: "Argument 'hour' passed in was null.");
253: }
254: if (minute == null) {
255: throw new IllegalArgumentException(
256: "Argument 'minute' passed in was null.");
257: }
258: if (second == null) {
259: throw new IllegalArgumentException(
260: "Argument 'second' passed in was null.");
261: }
262:
263: Calendar calendar = Calendar.getInstance();
264: calendar.set(Calendar.YEAR, year);
265: calendar.set(Calendar.MONTH, month);
266: calendar.set(Calendar.DAY_OF_MONTH, day);
267: calendar.set(Calendar.HOUR_OF_DAY, hour);
268: calendar.set(Calendar.MINUTE, minute);
269: calendar.set(Calendar.SECOND, second);
270: calendar.clear(Calendar.MILLISECOND);
271:
272: return new java.sql.Date(calendar.getTimeInMillis());
273: }
274: }
|