KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > DateUtils


1 package org.oddjob.schedules;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.GregorianCalendar JavaDoc;
6 import java.util.TimeZone JavaDoc;
7
8 /**
9  * Utility methods to do things with dates and intervals.
10  *
11  * @author Rob Gordon
12  */

13
14 public class DateUtils {
15
16     /**
17      * Private constructor as instantiating this utility class
18      * is meaningless.
19      */

20     private DateUtils() {
21     }
22
23     /**
24      * Calculate the start of day date time (i.e. at 00:00) for a given date.
25      *
26      * @param inDate The date to cacluate start of day from.
27      * @return The Date at the start of the day.
28      */

29     public static Date JavaDoc startOfDay(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
30         GregorianCalendar JavaDoc c1 = new GregorianCalendar JavaDoc();
31         c1.setTimeZone(timeZone);
32         c1.setTime(inDate);
33         GregorianCalendar JavaDoc c2 = new GregorianCalendar JavaDoc(
34                 c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
35                 c1.get(Calendar.DATE));
36         c2.setTimeZone(timeZone);
37         return c2.getTime();
38     }
39         
40     /**
41      * Calculate the date time at the end of the day (one millisecond before midnight)
42      * for the given date.
43      *
44      * @param inDate The given date.
45      * @return The Date at the end of the day.
46      */

47     public static Date JavaDoc endOfDay(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
48         Calendar JavaDoc inCalendar = new GregorianCalendar JavaDoc();
49         inCalendar.setTimeZone(timeZone);
50         inCalendar.setTime(inDate);
51         Calendar JavaDoc nextDay = new GregorianCalendar JavaDoc(
52                 inCalendar.get(Calendar.YEAR),
53                 inCalendar.get(Calendar.MONTH),
54                 inCalendar.get(Calendar.DAY_OF_MONTH) + 1);
55         nextDay.setTimeZone(timeZone);
56         return new Date JavaDoc(nextDay.getTime().getTime() - 1);
57     }
58     
59     /**
60      * Calculate the day number for the given date.
61      *
62      * @param inDate The given date.
63      * @return The day number.
64      */

65     public static int dayOfWeek(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
66         Calendar JavaDoc calendar = Calendar.getInstance(timeZone);
67         calendar.setTime(inDate);
68
69         return calendar.get(Calendar.DAY_OF_WEEK);
70     }
71     
72     /**
73      * Calcuate the month number (0 - 11) for the given date.
74      *
75      * @param inDate The given date.
76      * @return The month number.
77      */

78     public static int month(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
79         Calendar JavaDoc calendar = Calendar.getInstance();
80         calendar.setTimeZone(timeZone);
81         calendar.setTime(inDate);
82
83         return calendar.get(Calendar.MONTH);
84     }
85     
86     /**
87      * Calculate the day of the month (1 - 31) for the given date.
88      *
89      * @param inDate The given date.
90      * @return The day of the month.
91      */

92     public static int dayOfMonth(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
93         Calendar JavaDoc calendar = Calendar.getInstance();
94         calendar.setTimeZone(timeZone);
95         calendar.setTime(inDate);
96
97         return calendar.get(Calendar.DAY_OF_MONTH);
98     }
99     
100     /**
101      * Calculate the day of the year for the given
102      * date.
103      *
104      * @param forDate The date.
105      * @return The day of the year.
106      */

107     public static int dayOfYear(Date JavaDoc forDate, TimeZone JavaDoc timeZone) {
108         Calendar JavaDoc calendar = Calendar.getInstance();
109         calendar.setTimeZone(timeZone);
110         calendar.setTime(forDate);
111         return calendar.get(Calendar.DAY_OF_YEAR);
112     }
113             
114     /**
115      * Return a date which is 1 millisecond after the given date.
116      *
117      * @param date The given date.
118      * @return The date one millisecond later.
119      */

120     public static Date JavaDoc oneMillisAfter(Date JavaDoc date) {
121         if (date.getTime() == Interval.NEVER_AGAIN) {
122             return null;
123         }
124         return new Date JavaDoc(date.getTime() + 1);
125     }
126     
127     /**
128      * Return a date which is 1 millisecond before the given date.
129      *
130      * @param date The given date.
131      * @return The date one millisecond before.
132      */

133     public static Date JavaDoc oneMillisBefore(Date JavaDoc date) {
134         return new Date JavaDoc(date.getTime() - 1);
135     }
136
137     /**
138      * Compare to calendars.
139      *
140      * @param c1 First calendar.
141      * @param c2 Second calendar
142      * @return 1 if c1 > c2, 0 if c1 = c2, -1 if c1 < c2.
143      */

144     public static int compare(Calendar JavaDoc c1, Calendar JavaDoc c2) {
145         long m1 = c1.getTime().getTime();
146         long m2 = c2.getTime().getTime();
147         
148         if (m1 < m2) {
149             return -1;
150         }
151         if (m1 > m2) {
152             return 1;
153         }
154         return 0;
155     }
156 }
157
158
Popular Tags