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 31 32 final public class MonthSchedule extends ConstrainedSchedule implements Serializable { 33 34 private static final long serialVersionUID = 20050226; 35 36 private static final String MONTH_FORMAT = "MMM"; 37 38 private String from; 39 private String to; 40 41 45 public void setFrom(String from) { 46 this.from = from; 47 } 48 49 53 public String getFrom() { 54 return from; 55 } 56 57 61 public void setTo(String to) { 62 this.to = to; 63 } 64 65 69 public String getTo() { 70 return to; 71 } 72 73 80 static int parseMonth(String text, TimeZone timeZone) throws ParseException { 81 SimpleDateFormat f = new SimpleDateFormat (MONTH_FORMAT); 82 f.setTimeZone(timeZone); 83 Date d = f.parse(text); 84 return DateUtils.month(d, timeZone); 85 } 86 87 Calendar fromCalendar(Date referenceDate, TimeZone 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 e) { 98 throw new RuntimeException ("Failed to parse from day.", e); 99 } 100 } 101 } 102 103 Calendar toCalendar(Date referenceDate, TimeZone 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 e) { 114 throw new RuntimeException ("Failed to parse to day.", e); 115 } 116 } 117 } 118 119 123 public Date getStartDate(ScheduleContext context) { 124 Calendar fromCal = fromCalendar(context.getDate(), context.getTimeZone()); 125 Calendar 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 156 public Date getEndDate(ScheduleContext context) { 157 Calendar 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 |