Menu

Months within period with Joda-Time

Goal

To calculate the months within a given period

Description

This recipe tells you how to use Joda-Time to get the months that exist between two different dates (start and end).

How to

Implement a static function that gets two dates: start and end and returns a List of YearMonth objects. This function does nor care if there is exactly 1 month between the two dates. Instead, all we want is to return the months that goes from the first date to the last date. So, for instance, if the first date is 2014-01-31 and the second date is 2014-02-01, the result we want is 2014-01 (January of 2014) and 2014-02 (February of 2014).

...
public static List getYearMonths(final Date startDate, final Date endDate) {
final List result = new ArrayList();
final DateMidnight start = new DateMidnight(startDate.getTime()).withDayOfMonth(1);
final DateMidnight end = new DateMidnight(endDate.getTime()).
plusMonths(1).withDayOfMonth(1).minusDays(1);
if (start.isAfter(end)) {
throw new IllegalArgumentException("start must be before end");
}
DateMidnight current = new DateMidnight(start.getMillis());
do {
final YearMonth yearMonth = new YearMonth(current.getYear(), current.getMonthOfYear());
result.add(yearMonth);
current = current.plusMonths(1);
} while (current.isBefore(end));
return result;
}
...

The previous function uses the first day of the first date’s month and the last day of the second date’s month to make sure the calculations will return what we want (notice that, in case you are using a specific timezone, then your new DateMidnight() dates should take that into account as well, through calling Joda’s DateTimeZone.forID()). The following test checks the validation of the previous method: