Here is my Java class. Just pass the start date and end date to the method.
package org.inewsnet.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.joda.time.LocalDate;
import org.joda.time.Period;
/**
* Methods to calculate biweekly, monthly, and quarterly date frequencies
* @author Richard Golebiowski
*
*/
public class DateTimeService {
/**
* Calculates the dates for the Biweekly frequency
* @param dtStart the tart date
* @param dtEnd the end date
* @return a list of dates
*/
public List<Date> getBiwekly(Date dtStart, Date dtEnd){
List <Date> lstDate = new ArrayList<Date>();
LocalDate start = new LocalDate(dtStart);
LocalDate end = new LocalDate(dtEnd);
Period period = Period.weeks(2);
Period current = Period.ZERO;
while (true) {
LocalDate candidate = start.plus(current);
if (candidate.isEqual(end) || candidate.isAfter(end)) {
lstDate.add(dtEnd);
return lstDate;
}
else {
lstDate.add(candidate.toDate());
}
current = current.plus(period);
}
}
/**
* Calculates the dates for the Monthly frequency
* @param dtStart the tart date
* @param dtEnd the end date
* @return a list of dates
*/
public List<Date> getMonthly(Date dtStart, Date dtEnd){
List <Date> lstDate = new ArrayList<Date>();
LocalDate start = new LocalDate(dtStart);
LocalDate end = new LocalDate(dtEnd);
Period period = Period.months(1);
Period current = Period.ZERO;
while (true) {
LocalDate candidate = start.plus(current);
if (candidate.isEqual(end) || candidate.isAfter(end)) {
lstDate.add(dtEnd);
return lstDate;
}
else {
lstDate.add(candidate.toDate());
}
current = current.plus(period);
}
}
/**
* Calculates the dates for the Quarterly frequency
* @param dtStart the tart date
* @param dtEnd the end date
* @return a list of dates
*/
public List<Date> getQuarterly(Date dtStart, Date dtEnd){
List <Date> lstDate = new ArrayList<Date>();
LocalDate start = new LocalDate(dtStart);
LocalDate end = new LocalDate(dtEnd);
Period period = Period.weeks(13);
Period current = Period.ZERO;
while (true) {
LocalDate candidate = start.plus(current);
if (candidate.isEqual(end) || candidate.isAfter(end)) {
lstDate.add(dtEnd);
return lstDate;
}
else {
lstDate.add(candidate.toDate());
}
current = current.plus(period);
}
}
}
Great !!
ReplyDelete