public static CoveringInfo getYearlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 12) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); int beginOffset = (12 - fromMonth % 12) % 12; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 12; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 12, coverable); }
public static CoveringInfo getYearlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 12) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); int beginOffset = (12 - fromMonth % 12) % 12; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 12; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 12, coverable); }
public static CoveringInfo getQuarterlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 3) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); // Get the start date of the quarter int beginOffset = (3 - fromMonth % 3) % 3; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 3; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 3, coverable); }
public static CoveringInfo getQuarterlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 3) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); // Get the start date of the quarter int beginOffset = (3 - fromMonth % 3) % 3; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 3; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 3, coverable); }
@Test public void testMonthsBetween() throws Exception { int i = 0; assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], MONTH)), new CoveringInfo(1, true), "2013-Jan-01 to 2013-Jan-31"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], MONTH)), new CoveringInfo(5, true), "2013-Jan-01 to 2013-May-31"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], MONTH)), new CoveringInfo(12, true), "2013-Jan-01 to 2013-Dec-31"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(2, false), "2013-Feb-01 to 2013-Apr-25"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(12, true), "2012-Feb-01 to 2013-Feb-01"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(24, true), "2011-Feb-01 to 2013-Feb-01"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "2013-Jan-02 to 2013-Feb-02"); i += 2; assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(1, false), "2013-Jan-02 to 2013-Mar-02"); }
static CoveringInfo getCoveringInfo(Date from, Date to, UpdatePeriod interval) { switch (interval) { case SECONDLY: case CONTINUOUS: return getMilliSecondCoveringInfo(from, to, 1000, interval); case MINUTELY: case HOURLY: case DAILY: return getMilliSecondCoveringInfo(from, to, interval.weight(), interval); case WEEKLY: return getWeeklyCoveringInfo(from, to); case MONTHLY: return getMonthlyCoveringInfo(from, to); case QUARTERLY: return getQuarterlyCoveringInfo(from, to); case YEARLY: return getYearlyCoveringInfo(from, to); default: return new CoveringInfo(0, false); } }
static CoveringInfo getCoveringInfo(Date from, Date to, UpdatePeriod interval) { switch (interval) { case SECONDLY: case CONTINUOUS: return getMilliSecondCoveringInfo(from, to, 1000, interval); case MINUTELY: case HOURLY: case DAILY: return getMilliSecondCoveringInfo(from, to, interval.weight(), interval); case WEEKLY: return getWeeklyCoveringInfo(from, to); case MONTHLY: return getMonthlyCoveringInfo(from, to); case QUARTERLY: return getQuarterlyCoveringInfo(from, to); case YEARLY: return getYearlyCoveringInfo(from, to); default: return new CoveringInfo(0, false); } }