1 package org.oddjob.schedules.schedules; 2 3 import java.io.Serializable ; 4 import java.text.ParseException ; 5 import java.util.Calendar ; 6 import java.util.Date ; 7 import java.util.TimeZone ; 8 9 import org.oddjob.OddjobException; 10 import org.oddjob.schedules.ConstrainedSchedule; 11 import org.oddjob.schedules.DateUtils; 12 import org.oddjob.schedules.ScheduleContext; 13 import org.oddjob.util.DateHelper; 14 15 48 49 final public class TimeSchedule extends ConstrainedSchedule implements Serializable { 50 private static final long serialVersionUID = 20050226; 51 52 private String from; 53 private String to; 54 55 62 public void setFrom(String from) { 63 this.from = from; 64 } 65 66 70 public String getFrom() { 71 return from; 72 } 73 74 82 public void setTo(String to) { 83 this.to = to; 84 } 85 86 90 public String getTo() { 91 return to; 92 } 93 94 static Date parseTime(String textField, Date referenceDate, 95 TimeZone timeZone, String fieldName) { 96 String dateText = DateHelper.formatDate(referenceDate); 97 try { 98 return DateHelper.parseDateTime(dateText + " " + textField, timeZone); 99 } catch (ParseException e) { 100 throw new OddjobException("Failed to parse " + fieldName 101 + "[" + textField + "]"); 102 } 103 } 104 105 Calendar fromCalendar(Date referenceDate, TimeZone timeZone) { 106 Calendar fromCal = Calendar.getInstance(timeZone); 107 if (from == null) { 108 fromCal.setTime(DateUtils.startOfDay(referenceDate, timeZone)); 109 } 110 else { 111 fromCal.setTime(parseTime(from, referenceDate, timeZone, "from")); 112 } 113 return fromCal; 114 } 115 116 Calendar toCalendar(Date referenceDate, TimeZone timeZone) { 117 Calendar toCal = Calendar.getInstance(timeZone); 118 if (to == null) { 119 toCal.setTime(DateUtils.endOfDay(referenceDate, timeZone)); 120 } 121 else { 122 toCal.setTime(parseTime(to, referenceDate, timeZone, "to")); 123 } 124 return toCal; 125 } 126 127 128 132 public Date getStartDate(ScheduleContext context) { 133 Calendar fromCal = fromCalendar(context.getDate(), context.getTimeZone()); 134 Calendar toCal = toCalendar(context.getDate(), context.getTimeZone()); 135 136 if (!fromCal.equals(toCal) && toCal.get(Calendar.MILLISECOND) == 0) { 137 toCal.add(Calendar.MILLISECOND, -1); 138 } 139 140 Calendar nowCal = Calendar.getInstance(context.getTimeZone()); 141 nowCal.setTime(context.getDate()); 142 143 if (DateUtils.compare(fromCal, toCal) > 0) { 150 if (DateUtils.compare(nowCal, toCal) > 0) { 151 return fromCal.getTime(); 152 } 153 else { 154 fromCal.add(Calendar.DATE, -1); 155 return fromCal.getTime(); 156 } 157 } 158 else { 159 if (DateUtils.compare(nowCal, toCal) > 0) { 160 fromCal.add(Calendar.DATE, 1); 161 return fromCal.getTime(); 162 } 163 else { 164 return fromCal.getTime(); 165 } 166 } 167 } 168 169 173 public Date getEndDate(ScheduleContext context) { 174 Calendar fromCal = fromCalendar(context.getDate(), context.getTimeZone()); 175 Calendar toCal = toCalendar(context.getDate(), context.getTimeZone()); 176 177 if (!fromCal.equals(toCal) && toCal.get(Calendar.MILLISECOND) == 0) { 178 toCal.add(Calendar.MILLISECOND, -1); 179 } 180 181 Calendar nowCal = Calendar.getInstance(context.getTimeZone()); 182 nowCal.setTime(context.getDate()); 183 184 if (DateUtils.compare(nowCal, toCal) > 0) { 187 toCal.add(Calendar.DATE, 1); 188 return toCal.getTime(); 189 } 190 else { 191 return toCal.getTime(); 192 } 193 } 194 195 } 196 | Popular Tags |