1 7 8 package com.ibm.icu.util; 9 10 import java.util.Date ; 11 12 19 public class EasterHoliday extends Holiday 20 { 21 28 public EasterHoliday(String name) 29 { 30 super(name, new EasterRule(0, false)); 31 } 32 33 42 public EasterHoliday(int daysAfter, String name) 43 { 44 super(name, new EasterRule(daysAfter, false)); 45 } 46 47 58 public EasterHoliday(int daysAfter, boolean orthodox, String name) 59 { 60 super(name, new EasterRule(daysAfter, orthodox)); 61 } 62 63 68 static public final EasterHoliday SHROVE_TUESDAY = new EasterHoliday(-48, "Shrove Tuesday"); 69 70 75 static public final EasterHoliday ASH_WEDNESDAY = new EasterHoliday(-47, "Ash Wednesday"); 76 77 82 static public final EasterHoliday PALM_SUNDAY = new EasterHoliday( -7, "Palm Sunday"); 83 84 89 static public final EasterHoliday MAUNDY_THURSDAY = new EasterHoliday( -3, "Maundy Thursday"); 90 91 96 static public final EasterHoliday GOOD_FRIDAY = new EasterHoliday( -2, "Good Friday"); 97 98 103 static public final EasterHoliday EASTER_SUNDAY = new EasterHoliday( 0, "Easter Sunday"); 104 105 110 static public final EasterHoliday EASTER_MONDAY = new EasterHoliday( 1, "Easter Monday"); 111 112 117 static public final EasterHoliday ASCENSION = new EasterHoliday( 39, "Ascension"); 118 119 124 static public final EasterHoliday PENTECOST = new EasterHoliday( 49, "Pentecost"); 125 126 131 static public final EasterHoliday WHIT_SUNDAY = new EasterHoliday( 49, "Whit Sunday"); 132 133 138 static public final EasterHoliday WHIT_MONDAY = new EasterHoliday( 50, "Whit Monday"); 139 140 145 static public final EasterHoliday CORPUS_CHRISTI = new EasterHoliday( 60, "Corpus Christi"); 146 } 147 148 class EasterRule implements DateRule { 149 public EasterRule(int daysAfterEaster, boolean isOrthodox) { 150 this.daysAfterEaster = daysAfterEaster; 151 if (isOrthodox) { 152 orthodox.setGregorianChange(new Date (Long.MAX_VALUE)); 153 calendar = orthodox; 154 } 155 } 156 157 160 public Date firstAfter(Date start) 161 { 162 return doFirstBetween(start, null); 163 } 164 165 169 public Date firstBetween(Date start, Date end) 170 { 171 return doFirstBetween(start, end); 172 } 173 174 177 public boolean isOn(Date date) 178 { 179 synchronized(calendar) { 180 calendar.setTime(date); 181 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); 182 183 calendar.setTime(computeInYear(calendar.getTime(), calendar)); 184 185 return calendar.get(Calendar.DAY_OF_YEAR) == dayOfYear; 186 } 187 } 188 189 192 public boolean isBetween(Date start, Date end) 193 { 194 return firstBetween(start, end) != null; } 196 197 private Date doFirstBetween(Date start, Date end) 198 { 199 202 synchronized(calendar) { 203 Date result = computeInYear(start, calendar); 205 206 208 if (result.before(start)) 211 { 212 calendar.setTime(start); 213 calendar.get(Calendar.YEAR); calendar.add(Calendar.YEAR, 1); 215 216 219 result = computeInYear(calendar.getTime(), calendar); 220 } 222 223 if (end != null && result.after(end)) { 224 return null; 226 } 227 return result; 228 } 229 } 230 231 240 private Date computeInYear(Date date, GregorianCalendar cal) 241 { 242 if (cal == null) cal = calendar; 243 244 synchronized(cal) { 245 cal.setTime(date); 246 247 int year = cal.get(Calendar.YEAR); 248 int g = year % 19; int i = 0; int j = 0; 252 if (cal.getTime().after( cal.getGregorianChange())) 253 { 254 int c = year / 100; 256 int h = (c - c/4 - (8*c+13)/25 + 19*g + 15) % 30; 257 i = h - (h/28)*(1 - (h/28)*(29/(h+1))*((21-g)/11)); 258 j = (year + year/4 + i + 2 - c + c/4) % 7; 259 } 260 else 261 { 262 i = (19*g + 15) % 30; 264 j = (year + year/4 + i) % 7; 265 } 266 int l = i - j; 267 int m = 3 + (l+40)/44; int d = l + 28 - 31*(m/4); 270 cal.clear(); 271 cal.set(Calendar.ERA, GregorianCalendar.AD); 272 cal.set(Calendar.YEAR, year); 273 cal.set(Calendar.MONTH, m-1); cal.set(Calendar.DATE, d); 275 cal.getTime(); cal.add(Calendar.DATE, daysAfterEaster); 277 278 return cal.getTime(); 279 } 280 } 281 282 private static GregorianCalendar gregorian = new GregorianCalendar(); 283 private static GregorianCalendar orthodox = new GregorianCalendar(); 284 285 private int daysAfterEaster; 286 private GregorianCalendar calendar = gregorian; 287 } 288 | Popular Tags |