Tuesday, May 20, 2008

getDatesBetween

This code will return a list of date in the long formate, for example: Fri May 16 14:13:38 CDT 2008

public static List getDatesBetween(Date fromDate, Date toDate)
{
Calendar calendar = Calendar.getInstance();
List dateList = new ArrayList();
if (fromDate.before(toDate))
{
Date dateCounter = fromDate;
dateList.add(fromDate);
while (dateCounter.before(toDate))
{
calendar.setTime(dateCounter);
int nextDate = calendar.get(Calendar.DATE) + 1;
calendar.set(Calendar.DATE, nextDate);
dateCounter = calendar.getTime();
dateList.add(dateCounter);
}
} else if (!fromDate.before(toDate) && !fromDate.after(toDate))
{
dateList.add(fromDate);
}
return dateList;
}


Test:

public void test_DatesBetween(){
Date fromDate = DateHelper.incrementDays(new Date(), -4);
Date toDate = new Date ();
log.warn(DateHelper.getDatesBetween(fromDate, toDate));
}