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 * Simple implementation of DateRule.
\r
17 * @draft ICU 2.8 (retainAll)
\r
18 * @provisional This API might change or be removed in a future release.
\r
20 public class SimpleDateRule implements DateRule
\r
23 * Construct a rule for a fixed date within a month
\r
25 * @param month The month in which this rule occurs (0-based).
\r
26 * @param dayOfMonth The date in that month (1-based).
\r
28 * @provisional This API might change or be removed in a future release.
\r
30 public SimpleDateRule(int month, int dayOfMonth)
\r
33 this.dayOfMonth = dayOfMonth;
\r
38 /* package */SimpleDateRule(int month, int dayOfMonth, Calendar cal)
\r
41 this.dayOfMonth = dayOfMonth;
\r
43 this.calendar = cal;
\r
47 * Construct a rule for a weekday within a month, e.g. the first Monday.
\r
49 * @param month The month in which this rule occurs (0-based).
\r
50 * @param dayOfMonth A date within that month (1-based).
\r
51 * @param dayOfWeek The day of the week on which this rule occurs.
\r
52 * @param after If true, this rule selects the first dayOfWeek
\r
53 * on or after dayOfMonth. If false, the rule selects
\r
54 * the first dayOfWeek on or before dayOfMonth.
\r
56 * @provisional This API might change or be removed in a future release.
\r
58 public SimpleDateRule(int month, int dayOfMonth, int dayOfWeek, boolean after)
\r
61 this.dayOfMonth = dayOfMonth;
\r
62 this.dayOfWeek = after ? dayOfWeek : -dayOfWeek;
\r
66 * Return the first occurrance of the event represented by this rule
\r
67 * that is on or after the given start date.
\r
69 * @param start Only occurrances on or after this date are returned.
\r
71 * @return The date on which this event occurs, or null if it
\r
72 * does not occur on or after the start date.
\r
74 * @see #firstBetween
\r
76 * @provisional This API might change or be removed in a future release.
\r
78 public Date firstAfter(Date start)
\r
80 return doFirstBetween(start, null);
\r
84 * Return the first occurrance of the event represented by this rule
\r
85 * that is on or after the given start date and before the given
\r
88 * @param start Only occurrances on or after this date are returned.
\r
89 * @param end Only occurrances before this date are returned.
\r
91 * @return The date on which this event occurs, or null if it
\r
92 * does not occur between the start and end dates.
\r
96 * @provisional This API might change or be removed in a future release.
\r
98 public Date firstBetween(Date start, Date end)
\r
100 // Pin to the min/max dates for this rule
\r
101 return doFirstBetween(start, end);
\r
105 * Checks whether this event occurs on the given date. This does
\r
106 * <em>not</em> take time of day into account; instead it checks
\r
107 * whether this event and the given date are on the same day.
\r
108 * This is useful for applications such as determining whether a given
\r
109 * day is a holiday.
\r
111 * @param date The date to check.
\r
112 * @return true if this event occurs on the given date.
\r
114 * @provisional This API might change or be removed in a future release.
\r
116 public boolean isOn(Date date)
\r
118 Calendar c = calendar;
\r
123 int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
\r
125 c.setTime(computeInYear(c.get(Calendar.YEAR), c));
\r
127 // System.out.println(" isOn: dayOfYear = " + dayOfYear);
\r
128 // System.out.println(" holiday = " + c.get(Calendar.DAY_OF_YEAR));
\r
130 return c.get(Calendar.DAY_OF_YEAR) == dayOfYear;
\r
135 * Check whether this event occurs at least once between the two
\r
138 * @provisional This API might change or be removed in a future release.
\r
140 public boolean isBetween(Date start, Date end)
\r
142 return firstBetween(start, end) != null; // TODO: optimize?
\r
145 private Date doFirstBetween(Date start, Date end)
\r
147 Calendar c = calendar;
\r
152 int year = c.get(Calendar.YEAR);
\r
153 int mon = c.get(Calendar.MONTH);
\r
155 // If the rule is earlier in the year than the start date
\r
156 // we have to go to the next year.
\r
157 if (mon > this.month) {
\r
161 // Figure out when the rule lands in the given year
\r
162 Date result = computeInYear(year, c);
\r
164 // If the rule is in the same month as the start date, it's possible
\r
165 // to get a result that's before the start. If so, go to next year.
\r
166 if (mon == this.month && result.before(start)) {
\r
167 result = computeInYear(year+1, c);
\r
170 if (end != null && result.after(end)) {
\r
177 private Date computeInYear(int year, Calendar c)
\r
181 c.set(Calendar.ERA, c.getMaximum(Calendar.ERA));
\r
182 c.set(Calendar.YEAR, year);
\r
183 c.set(Calendar.MONTH, month);
\r
184 c.set(Calendar.DATE, dayOfMonth);
\r
186 //System.out.println(" computeInYear: start at " + c.getTime().toString());
\r
188 if (dayOfWeek != 0) {
\r
189 c.setTime(c.getTime()); // JDK 1.1.2 workaround
\r
190 int weekday = c.get(Calendar.DAY_OF_WEEK);
\r
192 //System.out.println(" weekday = " + weekday);
\r
193 //System.out.println(" dayOfYear = " + c.get(Calendar.DAY_OF_YEAR));
\r
196 if (dayOfWeek > 0) {
\r
197 // We want the first occurrance of the given day of the week
\r
198 // on or after the specified date in the month.
\r
199 delta = (dayOfWeek - weekday + 7) % 7;
\r
202 // We want the first occurrance of the (-dayOfWeek)
\r
203 // on or before the specified date in the month.
\r
204 delta = -((dayOfWeek + weekday + 7) % 7);
\r
206 //System.out.println(" adding " + delta + " days");
\r
207 c.add(Calendar.DATE, delta);
\r
210 return c.getTime();
\r
216 * @provisional This API might change or be removed in a future release.
\r
218 // public void setCalendar(Calendar c) {
\r
222 private static GregorianCalendar gCalendar = new GregorianCalendar();
\r
224 private Calendar calendar = gCalendar;
\r
227 private int dayOfMonth;
\r
228 private int dayOfWeek;
\r