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 28 29 30 final public class DayOfYearSchedule extends ConstrainedSchedule implements Serializable { 31 32 private static final long serialVersionUID = 20050226; 33 34 public static final String DAY_FORMAT = "dd-MMM"; 35 36 37 private String from; 38 39 40 private String to; 41 42 43 47 public void setFrom(String from) { 48 this.from = from; 49 } 50 51 55 public String getFrom() { 56 return from; 57 } 58 59 63 public void setTo(String to) { 64 this.to = to; 65 } 66 67 71 public String getTo() { 72 return to; 73 } 74 75 82 static int parseDay(String text, TimeZone timeZone) throws ParseException { 83 SimpleDateFormat f = new SimpleDateFormat (DAY_FORMAT); 84 f.setTimeZone(timeZone); 85 Date d = f.parse(text); 86 return DateUtils.dayOfYear(d, timeZone); 87 } 88 89 Calendar fromCalendar(Date referenceDate, TimeZone 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 e) { 100 throw new RuntimeException ("Failed to parse from day.", e); 101 } 102 } 103 } 104 105 Calendar toCalendar(Date referenceDate, TimeZone timeZone) { 106 if (to == null) { 107 return CalendarUtils.endOfYear(referenceDate, timeZone); 108 } 109 else { 110 try { 111 Calendar cal = CalendarUtils.dayOfYear(referenceDate, 112 parseDay(to, timeZone), 113 timeZone); 114 CalendarUtils.setEndOfDay(cal); 115 return cal; 116 } 117 catch (ParseException e) { 118 throw new RuntimeException ("Failed to parse to day.", e); 119 } 120 } 121 } 122 123 127 public Date getStartDate(ScheduleContext context) { 128 Calendar fromCal = fromCalendar(context.getDate(), context.getTimeZone()); 129 Calendar 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 160 public Date getEndDate(ScheduleContext context) { 161 Calendar 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 |