KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > schedules > DayOfYearSchedule


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.TimeZone JavaDoc;
9
10 import org.oddjob.schedules.CalendarUtils;
11 import org.oddjob.schedules.ConstrainedSchedule;
12 import org.oddjob.schedules.DateUtils;
13 import org.oddjob.schedules.ScheduleContext;
14
15 /**
16  * @oddjob.description A schedule for the days of the year.
17  * This will most frequently be used to define anual holidays.
18  *
19  * @oddjob.example
20  *
21  * <pre>
22  * &lt;dayofyear from="25-dec" to="26-dec"/&gt;
23  * &lt;dayofyear on="01-jan"/&gt;
24  * </pre>
25  *
26  * @author Rob Gordon
27  */

28
29
30 final public class DayOfYearSchedule extends ConstrainedSchedule implements Serializable JavaDoc {
31
32     private static final long serialVersionUID = 20050226;
33     
34     public static final String JavaDoc DAY_FORMAT = "dd-MMM";
35
36     /** The from day int as string. */
37     private String JavaDoc from;
38     
39     /** The to day int as a string. */
40     private String JavaDoc to;
41
42     
43     /*
44      * (non-Javadoc)
45      * @see org.treesched.ConstrainedSchedule#setFrom(java.lang.String)
46      */

47     public void setFrom(String JavaDoc from) {
48         this.from = from;
49     }
50
51     /*
52      * (non-Javadoc)
53      * @see org.treesched.ConstrainedSchedule#getFrom()
54      */

55     public String JavaDoc getFrom() {
56         return from;
57     }
58
59     /*
60      * (non-Javadoc)
61      * @see org.treesched.ConstrainedSchedule#setTo(java.lang.String)
62      */

63     public void setTo(String JavaDoc to) {
64         this.to = to;
65     }
66     
67     /*
68      * (non-Javadoc)
69      * @see org.treesched.ConstrainedSchedule#getTo()
70      */

71     public String JavaDoc getTo() {
72         return to;
73     }
74
75     /**
76      * Parse the day of the year.
77      *
78      * @param text The day of the year
79      * @param timeZone The time zone.
80      * @return
81      */

82     static int parseDay(String JavaDoc text, TimeZone JavaDoc timeZone) throws ParseException JavaDoc {
83         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(DAY_FORMAT);
84         f.setTimeZone(timeZone);
85         Date JavaDoc d = f.parse(text);
86         return DateUtils.dayOfYear(d, timeZone);
87     }
88     
89     Calendar JavaDoc fromCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
90         if (from == null) {
91             return CalendarUtils.startOfYear(referenceDate, timeZone);
92         }
93         else {
94             try {
95                 return CalendarUtils.dayOfYear(referenceDate,
96                             parseDay(from, timeZone),
97                             timeZone);
98             }
99             catch (ParseException JavaDoc e) {
100                 throw new RuntimeException JavaDoc("Failed to parse from day.", e);
101             }
102         }
103     }
104     
105     Calendar JavaDoc toCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
106         if (to == null) {
107             return CalendarUtils.endOfYear(referenceDate, timeZone);
108         }
109         else {
110             try {
111                 Calendar JavaDoc cal = CalendarUtils.dayOfYear(referenceDate,
112                             parseDay(to, timeZone),
113                             timeZone);
114                 CalendarUtils.setEndOfDay(cal);
115                 return cal;
116             }
117             catch (ParseException JavaDoc e) {
118                 throw new RuntimeException JavaDoc("Failed to parse to day.", e);
119             }
120         }
121     }
122     
123     /*
124      * (non-Javadoc)
125      * @see org.treesched.ConstrainedSchedule#getStartDate(java.util.Date)
126      */

127     public Date JavaDoc getStartDate(ScheduleContext context) {
128         Calendar JavaDoc fromCal = fromCalendar(context.getDate(), context.getTimeZone());
129         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
130                 
131         int nowDay = DateUtils.dayOfYear(context.getDate(), context.getTimeZone());
132         
133         int iFromDay = fromCal.get(Calendar.DAY_OF_YEAR);
134         int iToDay = toCal.get(Calendar.DAY_OF_YEAR);
135         
136         if (iFromDay > iToDay) {
137             if (nowDay > iToDay) {
138                 return fromCal.getTime();
139             }
140             else {
141                 fromCal.set(Calendar.YEAR, fromCal.get(Calendar.YEAR) - 1);
142                 return fromCal.getTime();
143             }
144         }
145         else {
146             if (nowDay > iToDay) {
147                 fromCal.set(Calendar.YEAR, fromCal.get(Calendar.YEAR) + 1);
148                 return fromCal.getTime();
149             }
150             else {
151                 return fromCal.getTime();
152             }
153         }
154     }
155     
156     /*
157      * (non-Javadoc)
158      * @see org.treesched.ConstrainedSchedule#getEndDate(java.util.Date)
159      */

160     public Date JavaDoc getEndDate(ScheduleContext context) {
161         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
162
163         int nowDay = DateUtils.dayOfYear(context.getDate(), context.getTimeZone());
164
165         int iToDay = toCal.get(Calendar.DAY_OF_YEAR);
166         
167         if (nowDay > iToDay) {
168             toCal.set(Calendar.YEAR, toCal.get(Calendar.YEAR) + 1);
169             return toCal.getTime();
170         }
171         else {
172             return toCal.getTime();
173         }
174     }
175 }
176
Popular Tags