KickJava   Java API By Example, From Geeks To Geeks.

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


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 a range of months, or a month.
17  *
18  * @oddjob.example
19  *
20  * <pre>
21  * &lt;month from="apr" to="sep" &gt;
22  * &lt;time on="10:00" /&gt;
23  * &lt;/month&gt;
24  * &lt;month from="oct" to="mar" &gt;
25  * &lt;time on="11:00" /&gt;
26  * &lt;/month&gt;
27  * </pre>
28  *
29  * @author Rob Gordon
30  */

31
32 final public class MonthSchedule extends ConstrainedSchedule implements Serializable JavaDoc {
33
34     private static final long serialVersionUID = 20050226;
35     
36     private static final String JavaDoc MONTH_FORMAT = "MMM";
37
38     private String JavaDoc from;
39     private String JavaDoc to;
40                 
41     /*
42      * (non-Javadoc)
43      * @see org.treesched.ConstrainedSchedule#setFrom(java.lang.String)
44      */

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

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

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

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

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

123     public Date JavaDoc getStartDate(ScheduleContext context) {
124         Calendar JavaDoc fromCal = fromCalendar(context.getDate(), context.getTimeZone());
125         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
126
127         int nowMonth = DateUtils.month(context.getDate(), context.getTimeZone());
128                 
129         int fromMonth = fromCal.get(Calendar.MONTH);
130         int toMonth = toCal.get(Calendar.MONTH);;
131         
132         if (fromMonth > toMonth) {
133             if (nowMonth > toMonth) {
134                 return fromCal.getTime();
135             }
136             else {
137                 fromCal.add(Calendar.YEAR, -1);
138                 return fromCal.getTime();
139             }
140         }
141         else {
142             if (nowMonth > toMonth) {
143                 fromCal.add(Calendar.YEAR, 1);
144                 return fromCal.getTime();
145             }
146             else {
147                 return fromCal.getTime();
148             }
149         }
150     }
151     
152     /*
153      * (non-Javadoc)
154      * @see org.treesched.ConstrainedSchedule#getEndDate(java.util.Date)
155      */

156     public Date JavaDoc getEndDate(ScheduleContext context) {
157         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
158         
159         int nowMonth = DateUtils.month(context.getDate(), context.getTimeZone());
160         
161         int toMonth = toCal.get(Calendar.MONTH);
162
163         if (nowMonth > toMonth) {
164             toCal.add(Calendar.YEAR, 1);
165         }
166         CalendarUtils.setEndOfMonth(toCal);
167         return toCal.getTime();
168
169     }
170 }
171
Popular Tags