2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import java.util.Date;
\r
13 * <b>Note:</b> The Holiday framework is a technology preview.
\r
14 * Despite its age, is still draft API, and clients should treat it as such.
\r
16 * A Holiday subclass which represents holidays that occur
\r
17 * a fixed number of days before or after Easter. Supports both the
\r
18 * Western and Orthodox methods for calculating Easter.
\r
19 * @draft ICU 2.8 (retainAll)
\r
20 * @provisional This API might change or be removed in a future release.
\r
22 public class EasterHoliday extends Holiday
\r
25 * Construct a holiday that falls on Easter Sunday every year
\r
27 * @param name The name of the holiday
\r
29 * @provisional This API might change or be removed in a future release.
\r
31 public EasterHoliday(String name)
\r
33 super(name, new EasterRule(0, false));
\r
37 * Construct a holiday that falls a specified number of days before
\r
38 * or after Easter Sunday each year.
\r
40 * @param daysAfter The number of days before (-) or after (+) Easter
\r
41 * @param name The name of the holiday
\r
43 * @provisional This API might change or be removed in a future release.
\r
45 public EasterHoliday(int daysAfter, String name)
\r
47 super(name, new EasterRule(daysAfter, false));
\r
51 * Construct a holiday that falls a specified number of days before
\r
52 * or after Easter Sunday each year, using either the Western
\r
53 * or Orthodox calendar.
\r
55 * @param daysAfter The number of days before (-) or after (+) Easter
\r
56 * @param orthodox Use the Orthodox calendar?
\r
57 * @param name The name of the holiday
\r
59 * @provisional This API might change or be removed in a future release.
\r
61 public EasterHoliday(int daysAfter, boolean orthodox, String name)
\r
63 super(name, new EasterRule(daysAfter, orthodox));
\r
67 * Shrove Tuesday, aka Mardi Gras, 48 days before Easter
\r
69 * @provisional This API might change or be removed in a future release.
\r
71 static public final EasterHoliday SHROVE_TUESDAY = new EasterHoliday(-48, "Shrove Tuesday");
\r
74 * Ash Wednesday, start of Lent, 47 days before Easter
\r
76 * @provisional This API might change or be removed in a future release.
\r
78 static public final EasterHoliday ASH_WEDNESDAY = new EasterHoliday(-47, "Ash Wednesday");
\r
81 * Palm Sunday, 7 days before Easter
\r
83 * @provisional This API might change or be removed in a future release.
\r
85 static public final EasterHoliday PALM_SUNDAY = new EasterHoliday( -7, "Palm Sunday");
\r
88 * Maundy Thursday, 3 days before Easter
\r
90 * @provisional This API might change or be removed in a future release.
\r
92 static public final EasterHoliday MAUNDY_THURSDAY = new EasterHoliday( -3, "Maundy Thursday");
\r
95 * Good Friday, 2 days before Easter
\r
97 * @provisional This API might change or be removed in a future release.
\r
99 static public final EasterHoliday GOOD_FRIDAY = new EasterHoliday( -2, "Good Friday");
\r
104 * @provisional This API might change or be removed in a future release.
\r
106 static public final EasterHoliday EASTER_SUNDAY = new EasterHoliday( 0, "Easter Sunday");
\r
109 * Easter Monday, 1 day after Easter
\r
111 * @provisional This API might change or be removed in a future release.
\r
113 static public final EasterHoliday EASTER_MONDAY = new EasterHoliday( 1, "Easter Monday");
\r
116 * Ascension, 39 days after Easter
\r
118 * @provisional This API might change or be removed in a future release.
\r
120 static public final EasterHoliday ASCENSION = new EasterHoliday( 39, "Ascension");
\r
123 * Pentecost (aka Whit Sunday), 49 days after Easter
\r
125 * @provisional This API might change or be removed in a future release.
\r
127 static public final EasterHoliday PENTECOST = new EasterHoliday( 49, "Pentecost");
\r
130 * Whit Sunday (aka Pentecost), 49 days after Easter
\r
132 * @provisional This API might change or be removed in a future release.
\r
134 static public final EasterHoliday WHIT_SUNDAY = new EasterHoliday( 49, "Whit Sunday");
\r
137 * Whit Monday, 50 days after Easter
\r
139 * @provisional This API might change or be removed in a future release.
\r
141 static public final EasterHoliday WHIT_MONDAY = new EasterHoliday( 50, "Whit Monday");
\r
144 * Corpus Christi, 60 days after Easter
\r
146 * @provisional This API might change or be removed in a future release.
\r
148 static public final EasterHoliday CORPUS_CHRISTI = new EasterHoliday( 60, "Corpus Christi");
\r
151 class EasterRule implements DateRule {
\r
152 public EasterRule(int daysAfterEaster, boolean isOrthodox) {
\r
153 this.daysAfterEaster = daysAfterEaster;
\r
155 orthodox.setGregorianChange(new Date(Long.MAX_VALUE));
\r
156 calendar = orthodox;
\r
161 * Return the first occurrance of this rule on or after the given date
\r
163 public Date firstAfter(Date start)
\r
165 return doFirstBetween(start, null);
\r
169 * Return the first occurrance of this rule on or after
\r
170 * the given start date and before the given end date.
\r
172 public Date firstBetween(Date start, Date end)
\r
174 return doFirstBetween(start, end);
\r
178 * Return true if the given Date is on the same day as Easter
\r
180 public boolean isOn(Date date)
\r
182 synchronized(calendar) {
\r
183 calendar.setTime(date);
\r
184 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
\r
186 calendar.setTime(computeInYear(calendar.getTime(), calendar));
\r
188 return calendar.get(Calendar.DAY_OF_YEAR) == dayOfYear;
\r
193 * Return true if Easter occurs between the two dates given
\r
195 public boolean isBetween(Date start, Date end)
\r
197 return firstBetween(start, end) != null; // TODO: optimize?
\r
200 private Date doFirstBetween(Date start, Date end)
\r
202 //System.out.println("doFirstBetween: start = " + start.toString());
\r
203 //System.out.println("doFirstBetween: end = " + end.toString());
\r
205 synchronized(calendar) {
\r
206 // Figure out when this holiday lands in the given year
\r
207 Date result = computeInYear(start, calendar);
\r
209 //System.out.println(" result = " + result.toString());
\r
211 // We might have gotten a date that's in the same year as "start", but
\r
212 // earlier in the year. If so, go to next year
\r
213 if (result.before(start))
\r
215 calendar.setTime(start);
\r
216 calendar.get(Calendar.YEAR); // JDK 1.1.2 bug workaround
\r
217 calendar.add(Calendar.YEAR, 1);
\r
219 //System.out.println(" Result before start, going to next year: "
\r
220 // + calendar.getTime().toString());
\r
222 result = computeInYear(calendar.getTime(), calendar);
\r
223 //System.out.println(" result = " + result.toString());
\r
226 if (end != null && result.after(end)) {
\r
227 //System.out.println("Result after end, returning null");
\r
235 * Compute the month and date on which this holiday falls in the year
\r
236 * containing the date "date". First figure out which date Easter
\r
237 * lands on in this year, and then add the offset for this holiday to get
\r
240 * The algorithm here is taken from the
\r
241 * <a href="http://www.faqs.org/faqs/calendars/faq/">Calendar FAQ</a>.
\r
243 private Date computeInYear(Date date, GregorianCalendar cal)
\r
245 if (cal == null) cal = calendar;
\r
247 synchronized(cal) {
\r
250 int year = cal.get(Calendar.YEAR);
\r
251 int g = year % 19; // "Golden Number" of year - 1
\r
252 int i = 0; // # of days from 3/21 to the Paschal full moon
\r
253 int j = 0; // Weekday (0-based) of Paschal full moon
\r
255 if (cal.getTime().after( cal.getGregorianChange()))
\r
257 // We're past the Gregorian switchover, so use the Gregorian rules.
\r
258 int c = year / 100;
\r
259 int h = (c - c/4 - (8*c+13)/25 + 19*g + 15) % 30;
\r
260 i = h - (h/28)*(1 - (h/28)*(29/(h+1))*((21-g)/11));
\r
261 j = (year + year/4 + i + 2 - c + c/4) % 7;
\r
265 // Use the old Julian rules.
\r
266 i = (19*g + 15) % 30;
\r
267 j = (year + year/4 + i) % 7;
\r
270 int m = 3 + (l+40)/44; // 1-based month in which Easter falls
\r
271 int d = l + 28 - 31*(m/4); // Date of Easter within that month
\r
274 cal.set(Calendar.ERA, GregorianCalendar.AD);
\r
275 cal.set(Calendar.YEAR, year);
\r
276 cal.set(Calendar.MONTH, m-1); // 0-based
\r
277 cal.set(Calendar.DATE, d);
\r
278 cal.getTime(); // JDK 1.1.2 bug workaround
\r
279 cal.add(Calendar.DATE, daysAfterEaster);
\r
281 return cal.getTime();
\r
285 private static GregorianCalendar gregorian = new GregorianCalendar(/* new SimpleTimeZone(0, "UTC") */);
\r
286 private static GregorianCalendar orthodox = new GregorianCalendar(/* new SimpleTimeZone(0, "UTC") */);
\r
288 private int daysAfterEaster;
\r
289 private GregorianCalendar calendar = gregorian;
\r