View Javadoc

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      public static Date makeDate(int year, int month, int day) {
21          calendar.clear();
22  
23          calendar.set(Calendar.YEAR, year);
24          calendar.set(Calendar.MONTH, month - 1);
25          calendar.set(Calendar.DATE, day);
26          calendar.set(Calendar.HOUR_OF_DAY, 0);
27          calendar.set(Calendar.MINUTE, 0);
28          calendar.set(Calendar.SECOND, 0);
29          calendar.set(Calendar.MILLISECOND, 0);
30  
31          return calendar.getTime();
32      }
33  
34      public static Date makeDate(
35          int year,
36          int month,
37          int day,
38          int hour,
39          int minute,
40          int second) {
41          calendar.clear();
42  
43          calendar.set(Calendar.YEAR, year);
44          calendar.set(Calendar.MONTH, month - 1);
45          calendar.set(Calendar.DATE, day);
46          calendar.set(Calendar.HOUR_OF_DAY, hour);
47          calendar.set(Calendar.MINUTE, minute);
48          calendar.set(Calendar.SECOND, second);
49          calendar.set(Calendar.MILLISECOND, 0);
50  
51          return calendar.getTime();
52      }
53      public static int getMonth(Date date) {
54          calendar.setTime(date);
55          return calendar.get(Calendar.MONTH) + 1;
56      }
57  
58      public static int getYear(Date date) {
59          calendar.setTime(date);
60          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      public static Date makeFutureDate(Date date, int daysLater) {
75          Calendar dateOnCalendar = Calendar.getInstance();
76          dateOnCalendar.setTime(date);
77          dateOnCalendar.add(Calendar.DATE, daysLater);
78          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      public static int computeAge(Date dateOfBirth, Date futureDate) {
91          Calendar birthdayDay = Calendar.getInstance();
92          birthdayDay.setTime(dateOfBirth);
93  
94          Calendar futureDay = Calendar.getInstance();
95          futureDay.setTime(futureDate);
96  
97          int yearDifference =
98              DateUtil.getYear(futureDate)
99                  - DateUtil.getYear(dateOfBirth);
100 
101         birthdayDay.roll(Calendar.YEAR, yearDifference);
102 
103         boolean notHadBirthdayYetThisYear =
104             birthdayDay.after(futureDay);
105 
106         int yearsAlive =
107             yearDifference - (notHadBirthdayYetThisYear ? 1 : 0);
108 
109         return yearsAlive;
110     }
111 
112 }