KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecCalendar


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.util.codec;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import java.lang.reflect.*;
40
41 /** calendar operations */
42
43 public class CodecCalendar extends Codec
44 {
45     /** monthly calendar keys */
46
47     public static final String JavaDoc NUMBEROFWEEKS = "numberOfWeeks";
48     public static final String JavaDoc WEEK = "week";
49     public static final String JavaDoc DAYINDEXSTART = "dayIndexStart";
50     public static final String JavaDoc DAYINDEXEND = "dayIndexEnd";
51     public static final String JavaDoc DAYINDEXTODAY = "dayIndexToday";
52     public static final String JavaDoc WEEKINDEX = "weekIndex";
53     public static final String JavaDoc DAYINDEX = "dayIndex";
54     public static final String JavaDoc DATE = "date";
55     public static final String JavaDoc TODAY = "today";
56     public static final String JavaDoc THISWEEK = "thisWeek";
57     public static final String JavaDoc FIRSTDAYOFMONTH = "firstDayOfMonth";
58     public static final String JavaDoc LASTDAYOFMONTH = "lastDayOfMonth";
59     public static final String JavaDoc NEXTMONTH = "nextMonth";
60     public static final String JavaDoc LASTMONTH = "lastMonth";
61     public static final String JavaDoc NEXTYEAR = "nextYear";
62     public static final String JavaDoc LASTYEAR = "lastYear";
63
64     private static int getFieldForName(String JavaDoc name) throws Exception JavaDoc
65     {
66         return Class.forName("java.util.Calendar").getField(name).getInt(null);
67     }
68
69     public static Object JavaDoc set(Object JavaDoc object, Object JavaDoc fieldSpec, Object JavaDoc valueSpec) throws Exception JavaDoc
70     {
71         if (object != null)
72         {
73             try
74             {
75                 Calendar calendar = TypeUtil.isCalendar(object, null, null, null);
76
77                 int field = getFieldForName(fieldSpec.toString());
78
79                 int value = TypeUtil.isInteger(valueSpec).intValue();
80             
81                 calendar.set(field, value);
82
83                 object = calendar;
84             }
85             catch (Exception JavaDoc e)
86             {
87                 object = null;
88             }
89         }
90
91         return object;
92     }
93
94     public static Object JavaDoc get(Object JavaDoc object, Object JavaDoc fieldSpec) throws Exception JavaDoc
95     {
96         if (object != null)
97         {
98             try
99             {
100                 Calendar calendar = TypeUtil.isCalendar(object, null, null, null);
101
102                 int field = getFieldForName(fieldSpec.toString());
103
104                 object = new Integer JavaDoc(calendar.get(field));
105             }
106             catch (Exception JavaDoc e)
107             {
108                 object = null;
109             }
110         }
111
112         return object;
113     }
114
115     public static Object JavaDoc add(Object JavaDoc object, Object JavaDoc fieldSpec, Object JavaDoc valueSpec) throws Exception JavaDoc
116     {
117         if (object != null)
118         {
119             try
120             {
121                 Calendar calendar = TypeUtil.isCalendar(object, null, null, null);
122
123                 int field = getFieldForName(fieldSpec.toString());
124
125                 int value = TypeUtil.isInteger(valueSpec).intValue();
126             
127                 calendar.add(field, value);
128
129                 object = calendar;
130             }
131             catch (Exception JavaDoc e)
132             {
133                 object = null;
134             }
135         }
136
137         return object;
138     }
139
140     public static Object JavaDoc roll(Object JavaDoc object, Object JavaDoc fieldSpec, Object JavaDoc valueSpec) throws Exception JavaDoc
141     {
142         if (object != null)
143         {
144             try
145             {
146                 Calendar calendar = TypeUtil.isCalendar(object, null, null, null);
147
148                 int field = getFieldForName(fieldSpec.toString());
149
150                 int value = TypeUtil.isInteger(valueSpec).intValue();
151             
152                 calendar.roll(field, value);
153
154                 object = calendar;
155             }
156             catch (Exception JavaDoc e)
157             {
158                 object = null;
159             }
160         }
161
162         return object;
163     }
164
165     /** returns monthly calendar map structure */
166
167     public static Object JavaDoc monthly(Object JavaDoc object) throws Exception JavaDoc
168     {
169         NestedMap monthly = null;
170
171         if (object != null)
172         {
173             try
174             {
175                 monthly = new NestedMap();
176
177                 Calendar calendar = TypeUtil.isCalendar(object, null, null, null);
178
179                 int firstDayOfWeek = calendar.getFirstDayOfWeek();
180             
181                 Date today = calendar.getTime();
182
183                 int thisWeek = calendar.get(Calendar.WEEK_OF_MONTH);
184
185                 calendar.set(Calendar.DATE, 1);
186                 calendar.add(Calendar.MONTH, 1);
187                 calendar.add(Calendar.DATE, -1);
188                 
189                 Date lastDayOfMonth = calendar.getTime();
190
191                 int lastWeek = calendar.get(Calendar.WEEK_OF_MONTH);
192                 
193                 calendar.set(Calendar.DATE, 1);
194             
195                 Date firstDayOfMonth = calendar.getTime();
196                 
197                 int firstWeek = calendar.get(Calendar.WEEK_OF_MONTH);
198                 
199                 calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
200                 
201                 Date firstDayOfCalendar = calendar.getTime();
202                 
203                 int numberOfWeeks = lastWeek - firstWeek + 1;
204
205                 monthly.put(NUMBEROFWEEKS, new Integer JavaDoc(numberOfWeeks));
206                 
207                 for (int w = 0; w < numberOfWeeks; w ++)
208                 {
209                     NestedMap weekMap = new NestedMap();
210                     
211                     weekMap.put(DAYINDEXSTART, new Integer JavaDoc(0));
212                     weekMap.put(DAYINDEXEND, new Integer JavaDoc(6));
213                     weekMap.put(WEEKINDEX, new Integer JavaDoc(w));
214                     
215                     for (int i = 0; i < 7; i ++)
216                     {
217                         Date day = calendar.getTime();
218                         
219                         weekMap.put("@_.*", day);
220                         calendar.add(Calendar.DATE, 1);
221                         
222                         if (day.equals(today))
223                         {
224                             NestedMap specialDay = new NestedMap();
225                             
226                             specialDay.put(DATE, day);
227                             specialDay.put(WEEKINDEX, new Integer JavaDoc(w));
228                             specialDay.put(DAYINDEX, new Integer JavaDoc(i));
229                             
230                             monthly.put(TODAY, specialDay);
231                             weekMap.put(DAYINDEXTODAY, new Integer JavaDoc(i));
232                             
233                             monthly.put(THISWEEK, weekMap);
234                         }
235                         
236                         if (day.equals(firstDayOfMonth))
237                         {
238                             NestedMap specialDay = new NestedMap();
239                             
240                             specialDay.put(DATE, day);
241                             specialDay.put(WEEKINDEX, new Integer JavaDoc(w));
242                             specialDay.put(DAYINDEX, new Integer JavaDoc(i));
243                             
244                             monthly.put(FIRSTDAYOFMONTH, specialDay);
245
246                             weekMap.put(DAYINDEXSTART, new Integer JavaDoc(i));
247                         }
248                         
249                         if (day.equals(lastDayOfMonth))
250                         {
251                             NestedMap specialDay = new NestedMap();
252                             
253                             specialDay.put(DATE, day);
254                             specialDay.put(WEEKINDEX, new Integer JavaDoc(w));
255                             specialDay.put(DAYINDEX, new Integer JavaDoc(i));
256                             
257                             monthly.put(LASTDAYOFMONTH, specialDay);
258                             
259                             weekMap.put(DAYINDEXEND, new Integer JavaDoc(i));
260                         }
261                     }
262                     
263                     monthly.put("@" + WEEK + "._.*", weekMap);
264                 }
265                 
266                 calendar.setTime(today);
267                 
268                 calendar.add(Calendar.MONTH, 1);
269
270                 monthly.put(NEXTMONTH, calendar.getTime());
271                 
272                 calendar.setTime(today);
273
274                 calendar.add(Calendar.MONTH, -1);
275
276                 monthly.put(LASTMONTH, calendar.getTime());
277                 
278                 calendar.setTime(today);
279             
280                 calendar.add(Calendar.YEAR, 1);
281
282                 monthly.put(NEXTYEAR, calendar.getTime());
283                 
284                 calendar.setTime(today);
285                 
286                 calendar.add(Calendar.YEAR, -1);
287
288                 monthly.put(LASTYEAR, calendar.getTime());
289
290                 calendar.setTime(today);
291             }
292             catch (Exception JavaDoc e)
293             {
294                 monthly = null;
295             }
296         }
297
298         return monthly;
299     }
300 }
301
302
Popular Tags