1 34 35 package com.micronova.util.codec; 36 37 import com.micronova.util.*; 38 import java.util.*; 39 import java.lang.reflect.*; 40 41 42 43 public class CodecCalendar extends Codec 44 { 45 46 47 public static final String NUMBEROFWEEKS = "numberOfWeeks"; 48 public static final String WEEK = "week"; 49 public static final String DAYINDEXSTART = "dayIndexStart"; 50 public static final String DAYINDEXEND = "dayIndexEnd"; 51 public static final String DAYINDEXTODAY = "dayIndexToday"; 52 public static final String WEEKINDEX = "weekIndex"; 53 public static final String DAYINDEX = "dayIndex"; 54 public static final String DATE = "date"; 55 public static final String TODAY = "today"; 56 public static final String THISWEEK = "thisWeek"; 57 public static final String FIRSTDAYOFMONTH = "firstDayOfMonth"; 58 public static final String LASTDAYOFMONTH = "lastDayOfMonth"; 59 public static final String NEXTMONTH = "nextMonth"; 60 public static final String LASTMONTH = "lastMonth"; 61 public static final String NEXTYEAR = "nextYear"; 62 public static final String LASTYEAR = "lastYear"; 63 64 private static int getFieldForName(String name) throws Exception 65 { 66 return Class.forName("java.util.Calendar").getField(name).getInt(null); 67 } 68 69 public static Object set(Object object, Object fieldSpec, Object valueSpec) throws Exception 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 e) 86 { 87 object = null; 88 } 89 } 90 91 return object; 92 } 93 94 public static Object get(Object object, Object fieldSpec) throws Exception 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 (calendar.get(field)); 105 } 106 catch (Exception e) 107 { 108 object = null; 109 } 110 } 111 112 return object; 113 } 114 115 public static Object add(Object object, Object fieldSpec, Object valueSpec) throws Exception 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 e) 132 { 133 object = null; 134 } 135 } 136 137 return object; 138 } 139 140 public static Object roll(Object object, Object fieldSpec, Object valueSpec) throws Exception 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 e) 157 { 158 object = null; 159 } 160 } 161 162 return object; 163 } 164 165 166 167 public static Object monthly(Object object) throws Exception 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 (numberOfWeeks)); 206 207 for (int w = 0; w < numberOfWeeks; w ++) 208 { 209 NestedMap weekMap = new NestedMap(); 210 211 weekMap.put(DAYINDEXSTART, new Integer (0)); 212 weekMap.put(DAYINDEXEND, new Integer (6)); 213 weekMap.put(WEEKINDEX, new Integer (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 (w)); 228 specialDay.put(DAYINDEX, new Integer (i)); 229 230 monthly.put(TODAY, specialDay); 231 weekMap.put(DAYINDEXTODAY, new Integer (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 (w)); 242 specialDay.put(DAYINDEX, new Integer (i)); 243 244 monthly.put(FIRSTDAYOFMONTH, specialDay); 245 246 weekMap.put(DAYINDEXSTART, new Integer (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 (w)); 255 specialDay.put(DAYINDEX, new Integer (i)); 256 257 monthly.put(LASTDAYOFMONTH, specialDay); 258 259 weekMap.put(DAYINDEXEND, new Integer (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 e) 293 { 294 monthly = null; 295 } 296 } 297 298 return monthly; 299 } 300 } 301 302 | Popular Tags |