Commits

timemachine committed ab3fbe4

Added LastMonthDateListProvider and couple utils in Schedules.

Comments (0)

Files changed (2)

timemachine-scheduler/src/main/java/timemachine/scheduler/Schedules.java

 			schedule.addDate(date);
 		return schedule;
 	}
+	
+	/** Return the last day of the month for given date. */
+	public static Date lastOfMonth(Date date) {
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(date);
+		cal.add(Calendar.MONTH, 1);
+		cal.set(Calendar.DAY_OF_MONTH, 1);
+		cal.add(Calendar.DAY_OF_MONTH, -1);
+		return cal.getTime();
+	}
+	
+	/** Return the first day of the month for given date. */
+	public static Date firstOfMonth(Date date) {
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(date);
+		cal.set(Calendar.DAY_OF_MONTH, 1);
+		return cal.getTime();
+	}
 }

timemachine-scheduler/src/main/java/timemachine/scheduler/schedule/LastMonthDateListProvider.java

+/*
+ * Copyright 2012 Zemian Deng
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package timemachine.scheduler.schedule;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import timemachine.scheduler.Schedules;
+import timemachine.scheduler.schedule.DateListSchedule;
+import timemachine.scheduler.schedule.DateListSchedule.DateListProvider;
+
+/**
+ * A provider that always returns the end of month date from prevRunDate. If prevRunDate is null, then end of 
+ * current month.
+ * @author Zemian Deng
+ */
+public class LastMonthDateListProvider implements DateListProvider {
+
+	@Override
+	public List<Date> getDateList(DateListSchedule schedule) {
+		List<Date> result = new ArrayList<Date>();
+		
+		Date prevDate = schedule.getPrevRun();
+		if (prevDate == null)
+			result.add(Schedules.lastOfMonth(new Date()));
+		else
+			result.add(Schedules.lastOfMonth(Schedules.addMonths(prevDate, 1)));
+		
+		return result;
+	}
+}