Clover coverage report - Diasparsoft Toolkit - 0.22
Coverage timestamp: Tue Jun 8 2004 12:41:26 EDT
file stats: LOC: 113   Methods: 6
NCLOC: 62   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DateUtil.java 0% 31.4% 33.3% 30.2%
coverage coverage
 1   
 package com.diasparsoftware.java.util;
 2   
 
 3   
 import java.util.*;
 4   
 
 5   
 public class DateUtil {
 6   
     private static Calendar calendar = new GregorianCalendar();
 7   
     /**
 8   
      * Create a <code>Date</code> object from the parameters you
 9   
      * expect to pass -- none of this "year minus 1900" and "months
 10   
      * start from 0" nonsense and we assume Gregorian!
 11   
      * 
 12   
      * @param year
 13   
      *            The real year: do not subtract 1900
 14   
      * @param month
 15   
      *            The real month: do not subtract 1
 16   
      * @param day
 17   
      *            Yes, we call it "day" and not "date"
 18   
      * @return
 19   
      */
 20  33
     public static Date makeDate(int year, int month, int day) {
 21  33
         calendar.clear();
 22   
 
 23  33
         calendar.set(Calendar.YEAR, year);
 24  33
         calendar.set(Calendar.MONTH, month - 1);
 25  33
         calendar.set(Calendar.DATE, day);
 26  33
         calendar.set(Calendar.HOUR_OF_DAY, 0);
 27  33
         calendar.set(Calendar.MINUTE, 0);
 28  33
         calendar.set(Calendar.SECOND, 0);
 29  33
         calendar.set(Calendar.MILLISECOND, 0);
 30   
 
 31  33
         return calendar.getTime();
 32   
     }
 33   
 
 34  0
     public static Date makeDate(
 35   
         int year,
 36   
         int month,
 37   
         int day,
 38   
         int hour,
 39   
         int minute,
 40   
         int second) {
 41  0
         calendar.clear();
 42   
 
 43  0
         calendar.set(Calendar.YEAR, year);
 44  0
         calendar.set(Calendar.MONTH, month - 1);
 45  0
         calendar.set(Calendar.DATE, day);
 46  0
         calendar.set(Calendar.HOUR_OF_DAY, hour);
 47  0
         calendar.set(Calendar.MINUTE, minute);
 48  0
         calendar.set(Calendar.SECOND, second);
 49  0
         calendar.set(Calendar.MILLISECOND, 0);
 50   
 
 51  0
         return calendar.getTime();
 52   
     }
 53  0
     public static int getMonth(Date date) {
 54  0
         calendar.setTime(date);
 55  0
         return calendar.get(Calendar.MONTH) + 1;
 56   
     }
 57   
 
 58  11
     public static int getYear(Date date) {
 59  11
         calendar.setTime(date);
 60  11
         return calendar.get(Calendar.YEAR);
 61   
     }
 62   
 
 63   
     /**
 64   
      * Make a date a specified number of days in the future from the
 65   
      * provided date.
 66   
      * 
 67   
      * @param date
 68   
      *            A benchmark date.
 69   
      * @param daysLater
 70   
      *            The number of days after the benchmark date you want
 71   
      *            to go.
 72   
      * @return
 73   
      */
 74  0
     public static Date makeFutureDate(Date date, int daysLater) {
 75  0
         Calendar dateOnCalendar = Calendar.getInstance();
 76  0
         dateOnCalendar.setTime(date);
 77  0
         dateOnCalendar.add(Calendar.DATE, daysLater);
 78  0
         return dateOnCalendar.getTime();
 79   
     }
 80   
 
 81   
     /**
 82   
      * Compute someone's age from their date of birth and a future
 83   
      * date. If you give a future date <em>not</em> in the future,
 84   
      * you will probably get a negative number.
 85   
      * 
 86   
      * @param dateOfBirth
 87   
      * @param futureDate
 88   
      * @return
 89   
      */
 90  0
     public static int computeAge(Date dateOfBirth, Date futureDate) {
 91  0
         Calendar birthdayDay = Calendar.getInstance();
 92  0
         birthdayDay.setTime(dateOfBirth);
 93   
 
 94  0
         Calendar futureDay = Calendar.getInstance();
 95  0
         futureDay.setTime(futureDate);
 96   
 
 97  0
         int yearDifference =
 98   
             DateUtil.getYear(futureDate)
 99   
                 - DateUtil.getYear(dateOfBirth);
 100   
 
 101  0
         birthdayDay.roll(Calendar.YEAR, yearDifference);
 102   
 
 103  0
         boolean notHadBirthdayYetThisYear =
 104   
             birthdayDay.after(futureDay);
 105   
 
 106  0
         int yearsAlive =
 107  0
             yearDifference - (notHadBirthdayYetThisYear ? 1 : 0);
 108   
 
 109  0
         return yearsAlive;
 110   
     }
 111   
 
 112   
 }
 113