tags:

views:

301

answers:

2

I am looking for a java library that when given from and to date would return a list of dates in weeks,months,quarter or year which ever is most applicable. I have done this manually and i wanted to know if this is already implemented and tested as a part of a standard package.

Example

Given 1/1/2009 , 1/4/2009 it should give 1/1/2009,1/2/2009,1/3/2009,1/4/2009

Given 1/1/2009 , 1/14/2009 it should give 1/1/2009,1/7/2009,1/14/2009

hope you that is clear :)

A: 

I'm currently using Joda Time for one of my projects, and have found it far more feature filled and usable than those found in the Java 6 API. I don't know if it has the exact feature you're looking for, however.

Jason Nichols
-1 for the default "use Joda" repsonse to any date/time related question without even knowing if it can solve the problem.
jarnbjo
+4  A: 

The DateTime class provided by Joda Time has methods such as plusDays(int), plusWeeks(int), plusMonths(int) which should help.

Assuming you want to get all the dates between start and end in weeks (pseudocode):

DateTime start = // whatever
DateTime end = // whatever

List<DateTime> datesBetween = new ArrayList<DateTime>();

while (start <= end) {
   datesBetween.add(start);
   DateTime dateBetween = start.plusWeeks(1);
   start = dateBetween;
}
Don