001/* 002 * Copyright 2006-2008 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */ 016package org.kuali.ole.sys.util; 017 018import java.sql.Timestamp; 019import java.util.Calendar; 020import java.util.Date; 021import java.util.GregorianCalendar; 022 023/** 024 * Utility methods for comparing dates 025 * 026 * 027 */ 028public class KfsDateUtils { 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 } 040 else if ((date1 != null) && (date2 != null)) { 041 return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2); 042 } 043 else { 044 same = false; 045 } 046 047 return same; 048 } 049 050 /** 051 * Adds null-safety to commons.DateUtils isSameDay method. 052 * 053 * @return true if both calendars are null or represent the same day 054 */ 055 public static boolean isSameDay(Calendar cal1, Calendar cal2) { 056 boolean same = false; 057 058 if ((cal1 == null) && (cal2 == null)) { 059 same = true; 060 } 061 else if ((cal1 != null) && (cal2 != null)) { 062 return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2); 063 } 064 else { 065 same = false; 066 } 067 068 return same; 069 } 070 071 /** 072 * Converts the given java.util.Date into an equivalent java.sql.Date 073 * 074 * @param date 075 * @return java.sql.Date constructed from the given java.util.Date 076 */ 077 public static java.sql.Date convertToSqlDate(java.util.Date date) { 078 return new java.sql.Date(date.getTime()); 079 } 080 081 082 /** 083 * Convert the given java.sql.date into a java.sql.date of which all the time fields are set to 0. 084 * 085 * @param date 086 * @return 087 */ 088 public static java.sql.Date clearTimeFields(java.sql.Date date) { 089 Calendar timelessCal = new GregorianCalendar(); 090 timelessCal.setTime(date); 091 timelessCal.set(Calendar.HOUR_OF_DAY, 0); 092 timelessCal.set(Calendar.MINUTE, 0); 093 timelessCal.set(Calendar.SECOND, 0); 094 timelessCal.set(Calendar.MILLISECOND, 0); 095 096 return new java.sql.Date(timelessCal.getTimeInMillis()); 097 } 098 099 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}