Coverage Report - org.kuali.rice.kns.util.DateUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DateUtils
0%
0/98
0%
0/34
4.111
 
 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.rice.kns.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  0
 public class DateUtils extends org.apache.commons.lang.time.DateUtils {
 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  0
         boolean same = false;
 36  
 
 37  0
         if ((date1 == null) && (date2 == null)) {
 38  0
             same = true;
 39  
         }
 40  0
         else if ((date1 != null) && (date2 != null)) {
 41  0
             return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
 42  
         }
 43  
         else {
 44  0
             same = false;
 45  
         }
 46  
 
 47  0
         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  0
         boolean same = false;
 57  
 
 58  0
         if ((cal1 == null) && (cal2 == null)) {
 59  0
             same = true;
 60  
         }
 61  0
         else if ((cal1 != null) && (cal2 != null)) {
 62  0
             return org.apache.commons.lang.time.DateUtils.isSameDay(cal1, cal2);
 63  
         }
 64  
         else {
 65  0
             same = false;
 66  
         }
 67  
 
 68  0
         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  0
         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  0
         Calendar timelessCal = new GregorianCalendar();
 90  0
         timelessCal.setTime(date);
 91  0
         timelessCal.set(Calendar.HOUR_OF_DAY, 0);
 92  0
         timelessCal.set(Calendar.MINUTE, 0);
 93  0
         timelessCal.set(Calendar.SECOND, 0);
 94  0
         timelessCal.set(Calendar.MILLISECOND, 0);
 95  
 
 96  0
         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  0
         Calendar timelessCal = new GregorianCalendar();
 108  0
         timelessCal.setTime(date);
 109  0
         timelessCal.set(Calendar.HOUR_OF_DAY, 0);
 110  0
         timelessCal.set(Calendar.MINUTE, 0);
 111  0
         timelessCal.set(Calendar.SECOND, 0);
 112  0
         timelessCal.set(Calendar.MILLISECOND, 0);
 113  
 
 114  0
         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  0
         int difference = 0;
 124  
 
 125  0
         Calendar startCalendar = Calendar.getInstance();
 126  0
         startCalendar.setTime(startDateTime);
 127  
 
 128  0
         Calendar endCalendar = Calendar.getInstance();
 129  0
         endCalendar.setTime(endDateTime);
 130  
 
 131  
         // First, get difference in whole days
 132  0
         Calendar startCompare = Calendar.getInstance();
 133  0
         startCompare.setTime(startDateTime);
 134  0
         startCompare.set(Calendar.HOUR_OF_DAY, 0);
 135  0
         startCompare.set(Calendar.MINUTE, 0);
 136  0
         startCompare.set(Calendar.SECOND, 0);
 137  0
         startCompare.set(Calendar.MILLISECOND, 0);
 138  
 
 139  0
         Calendar endCompare = Calendar.getInstance();
 140  0
         endCompare.setTime(endDateTime);
 141  0
         endCompare.set(Calendar.HOUR_OF_DAY, 0);
 142  0
         endCompare.set(Calendar.MINUTE, 0);
 143  0
         endCompare.set(Calendar.SECOND, 0);
 144  0
         endCompare.set(Calendar.MILLISECOND, 0);
 145  
 
 146  0
         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  0
         int difference = 0;
 156  
 
 157  0
         Calendar startCalendar = Calendar.getInstance();
 158  0
         startCalendar.setTime(startDateTime);
 159  
 
 160  0
         Calendar endCalendar = Calendar.getInstance();
 161  0
         endCalendar.setTime(endDateTime);
 162  
 
 163  
         // First, get difference in whole days
 164  0
         Calendar startCompare = Calendar.getInstance();
 165  0
         startCompare.setTime(startDateTime);
 166  0
         startCompare.set(Calendar.HOUR_OF_DAY, 0);
 167  0
         startCompare.set(Calendar.MINUTE, 0);
 168  
 
 169  0
         Calendar endCompare = Calendar.getInstance();
 170  0
         endCompare.setTime(endDateTime);
 171  0
         endCompare.set(Calendar.HOUR_OF_DAY, 0);
 172  0
         endCompare.set(Calendar.MINUTE, 0);
 173  
 
 174  0
         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  0
         if (year == null) {
 193  0
             throw new IllegalArgumentException("Argument 'year' passed in was null.");
 194  
         }
 195  0
         if (month == null) {
 196  0
             throw new IllegalArgumentException("Argument 'month' passed in was null.");
 197  
         }
 198  0
         if (day == null) {
 199  0
             throw new IllegalArgumentException("Argument 'day' passed in was null.");
 200  
         }
 201  
 
 202  0
         Calendar calendar = Calendar.getInstance();
 203  0
         calendar.set(Calendar.YEAR, year);
 204  0
         calendar.set(Calendar.MONTH, month);
 205  0
         calendar.set(Calendar.DAY_OF_MONTH, day);
 206  0
         calendar.clear(Calendar.HOUR_OF_DAY);
 207  0
         calendar.clear(Calendar.MINUTE);
 208  0
         calendar.clear(Calendar.SECOND);
 209  0
         calendar.clear(Calendar.MILLISECOND);
 210  
 
 211  0
         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  0
         if (year == null) {
 232  0
             throw new IllegalArgumentException("Argument 'year' passed in was null.");
 233  
         }
 234  0
         if (month == null) {
 235  0
             throw new IllegalArgumentException("Argument 'month' passed in was null.");
 236  
         }
 237  0
         if (day == null) {
 238  0
             throw new IllegalArgumentException("Argument 'day' passed in was null.");
 239  
         }
 240  0
         if (hour == null) {
 241  0
             throw new IllegalArgumentException("Argument 'hour' passed in was null.");
 242  
         }
 243  0
         if (minute == null) {
 244  0
             throw new IllegalArgumentException("Argument 'minute' passed in was null.");
 245  
         }
 246  0
         if (second == null) {
 247  0
             throw new IllegalArgumentException("Argument 'second' passed in was null.");
 248  
         }
 249  
 
 250  0
         Calendar calendar = Calendar.getInstance();
 251  0
         calendar.set(Calendar.YEAR, year);
 252  0
         calendar.set(Calendar.MONTH, month);
 253  0
         calendar.set(Calendar.DAY_OF_MONTH, day);
 254  0
         calendar.set(Calendar.HOUR_OF_DAY, hour);
 255  0
         calendar.set(Calendar.MINUTE, minute);
 256  0
         calendar.set(Calendar.SECOND, second);
 257  0
         calendar.clear(Calendar.MILLISECOND);
 258  
 
 259  0
         return new java.sql.Date(calendar.getTimeInMillis());
 260  
     }
 261  
 }