DateRangeCalculator { public static List<DateRange> fromMinMax(Date minDate, Date maxDate, String steppingPeriodLength, String compositingPeriodLength) { List<DateRange> dateRangeList = new ArrayList<>(); Period steppingPeriod = parsePeriod(steppingPeriodLength); Period compositingPeriod = parsePeriod(compositingPeriodLength); final GregorianCalendar calendar = DateUtils.createCalendar(); calendar.setTime(maxDate); calendar.add(Calendar.DAY_OF_MONTH, 1); final Date end = calendar.getTime(); calendar.setTime(minDate); while (true) { final Date periodStart = calendar.getTime(); compositingPeriod.next(calendar); calendar.add(Calendar.SECOND, -1); final Date periodEnd = calendar.getTime(); if (periodEnd.after(end)) { break; } dateRangeList.add(new DateRange(periodStart, periodEnd)); calendar.setTime(periodStart); steppingPeriod.next(calendar); } return dateRangeList; }  static List<DateRange> fromDateList(Date... dateList); static List<DateRange> fromMinMax(Date minDate, Date maxDate, String steppingPeriodLength, String compositingPeriodLength);  }
@Test public void testFromMinMax_5_5() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-04-05"), date("2002-04-25"), "5", "5"); assertNotNull(dateRanges); assertEquals(4, dateRanges.size()); assertEquals("[2002-04-05:2002-04-09]", dateRanges.get(0).toString()); assertEquals("[2002-04-10:2002-04-14]", dateRanges.get(1).toString()); assertEquals("[2002-04-15:2002-04-19]", dateRanges.get(2).toString()); assertEquals("[2002-04-20:2002-04-24]", dateRanges.get(3).toString()); }
@Test public void testFromMinMax_5_10() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-04-05"), date("2002-04-25"), "5", "10"); assertNotNull(dateRanges); assertEquals(3, dateRanges.size()); assertEquals("[2002-04-05:2002-04-14]", dateRanges.get(0).toString()); assertEquals("[2002-04-10:2002-04-19]", dateRanges.get(1).toString()); assertEquals("[2002-04-15:2002-04-24]", dateRanges.get(2).toString()); }
@Test public void testFromMinMax_10_5() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-04-05"), date("2002-04-25"), "10", "5"); assertNotNull(dateRanges); assertEquals(2, dateRanges.size()); assertEquals("[2002-04-05:2002-04-09]", dateRanges.get(0).toString()); assertEquals("[2002-04-15:2002-04-19]", dateRanges.get(1).toString()); }
@Test public void testFromMinMax_1w_1w_enfOfFeb() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-04-05"), date("2002-04-25"), "1w", "1w"); assertEquals(3, dateRanges.size()); assertEquals("[2002-04-05:2002-04-11]", dateRanges.get(0).toString()); assertEquals("[2002-04-12:2002-04-18]", dateRanges.get(1).toString()); assertEquals("[2002-04-19:2002-04-25]", dateRanges.get(2).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-20"), date("2004-03-10"), "7", "7"); assertEquals(2, dateRanges.size()); assertEquals("[2004-02-20:2004-02-26]", dateRanges.get(0).toString()); assertEquals("[2004-02-27:2004-03-04]", dateRanges.get(1).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-20"), date("2004-03-10"), "1w", "1w"); assertEquals(2, dateRanges.size()); assertEquals("[2004-02-20:2004-02-26]", dateRanges.get(0).toString()); assertEquals("[2004-02-27:2004-03-05]", dateRanges.get(1).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-21"), date("2004-03-05"), "1w", "1w"); assertEquals("[2004-02-21:2004-02-27]", dateRanges.get(0).toString()); assertEquals("[2004-02-28:2004-03-05]", dateRanges.get(1).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-22"), date("2004-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-02-22:2004-02-29]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-23"), date("2004-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-02-23:2004-03-01]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-02-24"), date("2004-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-02-24:2004-03-02]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-02-22"), date("2003-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-02-22:2003-02-28]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-02-23"), date("2003-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-02-23:2003-03-01]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-02-24"), date("2003-03-05"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-02-24:2003-03-02]", dateRanges.get(0).toString()); }
@Test public void testFromMinMax_1w_1w_enfOfDec() throws Exception { List<DateRange> dateRanges; dateRanges = DateRangeCalculator.fromMinMax( date("2004-12-22"), date("2005-01-04"), "1w", "1w"); assertEquals("[2004-12-22:2004-12-28]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-12-23"), date("2005-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-12-23:2004-12-29]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-12-24"), date("2005-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-12-24:2004-12-31]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-12-25"), date("2005-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-12-25:2005-01-01]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2004-12-26"), date("2005-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2004-12-26:2005-01-02]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-12-22"), date("2004-01-04"), "1w", "1w"); assertEquals("[2003-12-22:2003-12-28]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-12-23"), date("2004-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-12-23:2003-12-29]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-12-24"), date("2004-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-12-24:2003-12-31]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-12-25"), date("2004-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-12-25:2004-01-01]", dateRanges.get(0).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2003-12-26"), date("2004-01-04"), "1w", "1w"); assertEquals(1, dateRanges.size()); assertEquals("[2003-12-26:2004-01-02]", dateRanges.get(0).toString()); }
@Test public void testFromMinMax_1m_5d() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-01-01"), date("2002-04-30"), "1m", "5d"); assertEquals(4, dateRanges.size()); assertEquals("[2002-01-01:2002-01-05]", dateRanges.get(0).toString()); assertEquals("[2002-02-01:2002-02-05]", dateRanges.get(1).toString()); assertEquals("[2002-03-01:2002-03-05]", dateRanges.get(2).toString()); assertEquals("[2002-04-01:2002-04-05]", dateRanges.get(3).toString()); }
@Test public void testFromMinMax_1m_1m() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-01-01"), date("2002-04-30"), "1m", "1m"); assertEquals(4, dateRanges.size()); assertEquals("[2002-01-01:2002-01-31]", dateRanges.get(0).toString()); assertEquals("[2002-02-01:2002-02-28]", dateRanges.get(1).toString()); assertEquals("[2002-03-01:2002-03-31]", dateRanges.get(2).toString()); assertEquals("[2002-04-01:2002-04-30]", dateRanges.get(3).toString()); dateRanges = DateRangeCalculator.fromMinMax( date("2002-01-15"), date("2002-04-30"), "1m", "1m"); assertEquals(3, dateRanges.size()); assertEquals("[2002-01-15:2002-02-14]", dateRanges.get(0).toString()); assertEquals("[2002-02-15:2002-03-14]", dateRanges.get(1).toString()); assertEquals("[2002-03-15:2002-04-14]", dateRanges.get(2).toString()); }
@Test public void testFromMinMax_1y_1y() throws Exception { List<DateRange> dateRanges = DateRangeCalculator.fromMinMax( date("2002-01-01"), date("2004-12-31"), "1y", "1y"); assertEquals(3, dateRanges.size()); assertEquals("[2002-01-01:2002-12-31]", dateRanges.get(0).toString()); assertEquals("[2003-01-01:2003-12-31]", dateRanges.get(1).toString()); assertEquals("[2004-01-01:2004-12-31]", dateRanges.get(2).toString()); }