KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.schedules;
5
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.TimeZone JavaDoc;
9
10 public class CalendarUtils {
11
12     /**
13      * Set the calendar to the end of day.
14      *
15      * @param calendar The calendar that will be set.
16      */

17     public static void setEndOfDay(Calendar JavaDoc calendar) {
18         calendar.set(Calendar.HOUR_OF_DAY, 23);
19         calendar.set(Calendar.MINUTE, 59);
20         calendar.set(Calendar.SECOND, 59);
21         calendar.set(Calendar.MILLISECOND, 999);
22     }
23     
24     /**
25      * Set the calendar to the end of the month.
26      *
27      * @param calendar The calendar that will be set.
28      */

29     public static void setEndOfMonth(Calendar JavaDoc calendar) {
30         calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
31         calendar.set(Calendar.DAY_OF_MONTH, 0);
32         calendar.set(Calendar.HOUR_OF_DAY, 23);
33         calendar.set(Calendar.MINUTE, 59);
34         calendar.set(Calendar.SECOND, 59);
35         calendar.set(Calendar.MILLISECOND, 999);
36     }
37     
38     /**
39      * Calculate the date at the start of the month for the given date.
40      *
41      * @param inDate The given date.
42      * @return The date at the start of the month.
43      */

44     public static Calendar JavaDoc startOfMonth(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
45         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
46         c1.setTime(inDate);
47         
48         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
49         c2.clear();
50         c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), 1);
51         return c2;
52     }
53
54     /**
55      * Calculate the date at the end of the month for the given date.
56      *
57      * @param inDate The given date.
58      * @return The calendar at the end of the month.
59      */

60     public static Calendar JavaDoc endOfMonth(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
61         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
62         c1.setTime(inDate);
63         
64         // get the date at the beginning of next month
65
Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
66         c2.clear();
67         c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH) + 1, 1);
68         // and take off a millisecond.
69
c2.add(Calendar.MILLISECOND, -1);
70         return c2;
71     }
72     
73     /**
74      * Utility function to get a calendar which
75      * represents the day of the month in which the reference date is.
76      *
77      * @param referenceDate The date to take month from.
78      * @param day The day.
79      * @return The Calendar.
80      */

81     public static Calendar JavaDoc dayOfMonth(Date JavaDoc referenceDate, int day, TimeZone JavaDoc timeZone) {
82         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
83         c1.setTime(referenceDate);
84         
85         int month = day > 0 ? c1.get(Calendar.MONTH) : c1.get(Calendar.MONTH) + 1;
86         
87         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
88         c2.clear();
89         c2.set(c1.get(Calendar.YEAR), month, day);
90         return c2;
91     }
92         
93     /**
94      * Calculate the start of the week for the given date.
95      *
96      * @param inDate The given date.
97      *
98      * @return The date at the start of the month.
99      */

100     public static Calendar JavaDoc startOfWeek(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
101         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
102         c1.setTime(inDate);
103         
104         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
105         c2.clear();
106         c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
107                 c1.get(Calendar.DAY_OF_MONTH) - (c1.get(Calendar.DAY_OF_WEEK) - 1));
108         return c2;
109     }
110
111     /**
112      * Calculate the start of the week for the given date.
113      *
114      * @param inDate The given date.
115      *
116      * @return The date at the start of the month.
117      */

118     public static Calendar JavaDoc endOfWeek(Date JavaDoc inDate, TimeZone JavaDoc timeZone) {
119         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
120         c1.setTime(inDate);
121         
122         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
123         c2.clear();
124         c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
125                 c1.get(Calendar.DAY_OF_MONTH) + (8 - c1.get(Calendar.DAY_OF_WEEK)));
126         c2.add(Calendar.MILLISECOND, -1);
127         return c2;
128     }
129     
130     /**
131      * Utility function to get a calendar which
132      * represents the day of the week from the reference date.
133
134      * @param referenceDate The date to take week from.
135      * @param day The day.
136      * @return The Calendar.
137      */

138     public static Calendar JavaDoc dayOfWeek(Date JavaDoc referenceDate, int day, TimeZone JavaDoc timeZone) {
139         
140         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
141         c1.setTime(referenceDate);
142         
143         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
144         c2.clear();
145         c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
146                 c1.get(Calendar.DAY_OF_MONTH));
147         c2.add(Calendar.DATE, day - c1.get(Calendar.DAY_OF_WEEK));
148         return c2;
149     }
150     
151     /**
152      * Calculate the date at the start of the year for the given date.
153      *
154      * @param referenceDate The given date.
155      * @return The calendar at the start of the year.
156      */

157     public static Calendar JavaDoc startOfYear(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
158         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
159         c1.setTime(referenceDate);
160         
161         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
162         c2.clear();
163         c2.set(c1.get(Calendar.YEAR), 0, 1);
164         return c2;
165     }
166
167     /**
168      * Calcuate the date at the end of the year from the
169      * given date.
170      *
171      * @param referenceDate The given date.
172      * @return The calendar at the end of the year.
173      */

174     public static Calendar JavaDoc endOfYear(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
175         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
176         c1.setTime(referenceDate);
177         
178         Calendar JavaDoc c2 = Calendar.getInstance();
179         c2.clear();
180         c2.set(c1.get(Calendar.YEAR) + 1, 0, 1);
181         c2.setTimeZone(timeZone);
182         // and take off a millisecond.
183
c2.add(Calendar.MILLISECOND, -1);
184         return c2;
185     }
186
187
188     /**
189      * Calendar for the day of year.
190      *
191      * @param referenceDate The date to take year from.
192      * @param dayOfYear The day of the year.
193      * @return The calendar.
194      */

195     public static Calendar JavaDoc dayOfYear(Date JavaDoc referenceDate, int dayOfYear, TimeZone JavaDoc timeZone) {
196         Calendar JavaDoc c1 = Calendar.getInstance();
197         c1.setTimeZone(timeZone);
198         c1.setTime(referenceDate);
199         
200         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
201         c2.clear();
202         c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
203         c2.set(Calendar.DAY_OF_YEAR, dayOfYear);
204         return c2;
205     }
206
207     /**
208      * Utility function to get the start of the month as a Calendar.
209      *
210      * @param referenceDate The whole date.
211      * @param month The month to calculate the start of.
212      * @return The start of the month.
213      */

214     public static Calendar JavaDoc monthOfYear(Date JavaDoc referenceDate, int month, TimeZone JavaDoc timeZone) {
215         Calendar JavaDoc c1 = Calendar.getInstance(timeZone);
216         c1.setTime(referenceDate);
217         
218         Calendar JavaDoc c2 = Calendar.getInstance(timeZone);
219         c2.clear();
220         c2.set(c1.get(Calendar.YEAR), month, 1);
221         return c2;
222     }
223
224 }
225
Popular Tags