DateTimePeriodSelector { static public List<DateTimePeriod> last12Months(DateTimeZone zone) { ArrayList<DateTimePeriod> periods = new ArrayList<DateTimePeriod>(); DateTime now = new DateTime(zone); for (int i = 0; i < 12; i++) { DateTimePeriod period = DateTimePeriod.createMonth(now.getYear(), now.getMonthOfYear(), zone); periods.add(period); now = now.minusMonths(1); } Collections.reverse(periods); return periods; } private  DateTimePeriodSelector(); static DateTimePeriod thisMonth(DateTimeZone zone); static DateTimePeriod lastMonth(DateTimeZone zone); static List<DateTimePeriod> last12Months(DateTimeZone zone);  }
@Test public void last12Months() throws Exception { List<DateTimePeriod> periods = DateTimePeriodSelector.last12Months(DateTimeZone.UTC); DateTime now = new DateTime(DateTimeZone.UTC); DateTime startingMonth = now.minusMonths(12); DateTimePeriod startingPeriod = DateTimePeriod.createMonth(startingMonth.getYear(), startingMonth.getMonthOfYear(), DateTimeZone.UTC); ArrayList<DateTimePeriod> expectedPeriods = new ArrayList<DateTimePeriod>(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); startingPeriod = startingPeriod.getNext(); expectedPeriods.add(startingPeriod); Assert.assertArrayEquals(expectedPeriods.toArray(), periods.toArray()); }