1 package org.oddjob.schedules.schedules; 2 3 import java.io.Serializable ; 4 import java.text.ParseException ; 5 import java.text.SimpleDateFormat ; 6 import java.util.Calendar ; 7 import java.util.Date ; 8 import java.util.TimeZone ; 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 26 27 28 final public class DayOfWeekSchedule extends ConstrainedSchedule implements Serializable { 29 30 private static final long serialVersionUID = 20050226; 31 32 private final static String DAY_FORMAT = "EEE"; 33 34 35 private String from; 36 37 38 private String to; 39 40 48 public void setFrom(String from) { 49 this.from = from; 50 } 51 52 56 public String getFrom() { 57 return from; 58 } 59 60 68 public void setTo(String to) { 69 this.to = to; 70 } 71 72 76 public String getTo() { 77 return to; 78 } 79 80 87 static int parseDay(String text, TimeZone timeZone) throws ParseException { 88 SimpleDateFormat f = new SimpleDateFormat (DAY_FORMAT); 89 f.setTimeZone(timeZone); 90 Date d = f.parse(text); 91 return DateUtils.dayOfWeek(d, timeZone); 92 } 93 94 95 Calendar fromCalendar(Date referenceDate, TimeZone timeZone) { 96 if (from == null) { 97 return CalendarUtils.startOfWeek(referenceDate, timeZone); 98 } 99 else { 100 try { 101 return CalendarUtils.dayOfWeek(referenceDate, 102 parseDay(from, timeZone), 103 timeZone); 104 } 105 catch (ParseException e) { 106 throw new RuntimeException ("Failed to parse from day.", e); 107 } 108 } 109 } 110 111 Calendar toCalendar(Date referenceDate, TimeZone timeZone) { 112 if (to == null) { 113 return CalendarUtils.endOfWeek(referenceDate, timeZone); 114 } 115 else { 116 try { 117 Calendar cal = CalendarUtils.dayOfWeek(referenceDate, 118 parseDay(to, timeZone), 119 timeZone); 120 CalendarUtils.setEndOfDay(cal); 121 return cal; 122 } 123 catch (ParseException e) { 124 throw new RuntimeException ("Failed to parse to day.", e); 125 } 126 } 127 } 128 129 133 public Date getStartDate(ScheduleContext context) { 134 Calendar fromCal = fromCalendar(context.getDate(), context.getTimeZone()); 135 Calendar toCal = toCalendar(context.getDate(), context.getTimeZone()); 136 137 int fromDay = fromCal.get(Calendar.DAY_OF_WEEK); 138 int toDay = toCal.get(Calendar.DAY_OF_WEEK); 139 int nowDay = DateUtils.dayOfWeek(context.getDate(), context.getTimeZone()); 140 141 if (fromDay > toDay) { 142 if (nowDay > toDay) { 143 return fromCal.getTime(); 144 } 145 else { 146 fromCal.add(Calendar.DATE, -7); 147 return fromCal.getTime(); 148 } 149 } 150 else { 151 if (nowDay > toDay) { 152 fromCal.add(Calendar.DATE, 7); 153 return fromCal.getTime(); 154 } 155 else { 156 return fromCal.getTime(); 157 } 158 } 159 } 160 161 165 public Date getEndDate(ScheduleContext context) { 166 Calendar toCal = toCalendar(context.getDate(), context.getTimeZone()); 167 168 int nowDay = DateUtils.dayOfWeek(context.getDate(), context.getTimeZone()); 169 int toDay = toCal.get(Calendar.DAY_OF_WEEK); 170 171 if (nowDay > toDay) { 172 toCal.add(Calendar.DATE, 7); 173 return toCal.getTime(); 174 } 175 else { 176 return toCal.getTime(); 177 } 178 } 179 } 180 | Popular Tags |