It generates a list of dates from the given start date and end date for every month for the given week number(1st/2nd/3rd/4th week of the month) and for the given day (Sun,Mon,Tue ...Sat)
For example
Start date = 1/1/2012
end date = 31/3/2012
frequency = 1 (1st/2nd/3rd/4th week of the month)
day = monday (Sunday to saturday)
Output is
---------
2/1/2012
6/2/2012
5/3/2012
For example
Start date = 1/1/2012
end date = 31/3/2012
frequency = 1 (1st/2nd/3rd/4th week of the month)
day = monday (Sunday to saturday)
Output is
---------
2/1/2012
6/2/2012
5/3/2012
private List<Object> generateRepEveryDates(Cx cx, RepetitiveGroupInfo repetitiveGroupInfo) throws AppException {
//RepetitiveGroupInfo is a bean and it has the attributes of Start date, end date, day, frequency,
List<Object> repList = new ArrayList<Object>();
Calendar calendar = Calendar.getInstance();
//Get a start date
Date repStartDate = repetitiveGroupInfo.getStartDate();
//Set a end date
Date repEndDate = repetitiveGroupInfo.getEndDate();
//Keep startdate as temp date to be constant
Date tempDate = repStartDate;
//Its a day of week(Monday/Tuesday... Sunday)
int repFreqDaysValue = repetitiveGroupInfo.getDay().getId().intValue();
//Frequency is 1st/2nd/3rd/4th week of the month
int freqEveryValue = repetitiveGroupInfo.getFrequencyEvery().intValue();
tempDate = calculateDate(repFreqDaysValue, tempDate);
//calculating first date of repetitive date. it must ne greater than start date
tempDate = DateTool.add(tempDate, 7 * (freqEveryValue - 1), TimeUnit.DAY);
if (Val.isAfterOrEqual(tempDate, repStartDate)) {
repList.add(tempDate);
}
//calculating from second date of repetitive date till the end date
while (Val.isAfterOrEqual(repEndDate, tempDate)) {
tempDate = DateTool.add(tempDate, 1, TimeUnit.MONTH);
tempDate = calculateDate(repFreqDaysValue, tempDate);
calendar.setTime(tempDate);
int currentMonth = calendar.get(Calendar.MONTH);
tempDate = DateTool.add(tempDate, 7 * (freqEveryValue - 1), TimeUnit.DAY);
int afterAddedWeeks = calendar.get(Calendar.MONTH);
// checking whether last calsulated repetitive date is before the end date
if (Val.isAfterOrEqual(repEndDate, tempDate)) {
if (currentMonth == afterAddedWeeks) {
repList.add(tempDate);
}
}
}
return repList;
}
private Date calculateDate(int repFreqDaysValue, Date currentDate) throws AppException {
int index = 0;
Date calcDate = currentDate;
Calendar calendarObj = Calendar.getInstance();
calendarObj.setTime(calcDate);
calendarObj.set(Calendar.DATE, 1);
calcDate = calendarObj.getTime();
index = calendarObj.get(Calendar.DAY_OF_WEEK);
index = index - 1;
if (index > repFreqDaysValue) {
index = (7 - index) + repFreqDaysValue;
calcDate = DateTool.add(calcDate, index, TimeUnit.DAY);
} else if (index < repFreqDaysValue) {
index = repFreqDaysValue - index;
calcDate = DateTool.add(calcDate, index, TimeUnit.DAY);
}
return calcDate;
}