/** * Given a month (from 1 to 12) it calculates the first month of its quarter. * * @param firstMonthOfYear The first month of the year considered the first month of the first quarter. * @param targetMonth The month we want to evaluate to which quarter belongs. * @return The first month of the target quarter for the given month. */ public static int getQuarterFirstMonth(int firstMonthOfYear, int targetMonth) { int result = firstMonthOfYear; int index = firstMonthOfYear; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (index == targetMonth) return result; index = Month.nextIndex(index); } result = index; } throw new RuntimeException("Month not found : " + targetMonth); }
/** * Given a month (from 1 to 12) it calculates the ordinal within the quarter it belongs. * * @param firstMonthOfYear The first month of the year considered the first month of the first quarter. * @param targetMonth The month we want to evaluate. * @return The position (from 0 to 2) of the target within the quarter. */ public static int getPositionInQuarter(int firstMonthOfYear, int targetMonth) { int index = firstMonthOfYear; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (index == targetMonth) return j; index = Month.nextIndex(index); } } throw new RuntimeException("Month not found : " + targetMonth); } }
/** * Given a month (from 1 to 12) it calculates the ordinal within the quarter it belongs. * * @param firstMonthOfYear The first month of the year considered the first month of the first quarter. * @param targetMonth The month we want to evaluate. * @return The position (from 0 to 2) of the target within the quarter. */ public static int getPositionInQuarter(int firstMonthOfYear, int targetMonth) { int index = firstMonthOfYear; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (index == targetMonth) return j; index = Month.nextIndex(index); } } throw new RuntimeException("Month not found : " + targetMonth); } }
/** * Given a month (from 1 to 12) it calculates the first month of its quarter. * * @param firstMonthOfYear The first month of the year considered the first month of the first quarter. * @param targetMonth The month we want to evaluate to which quarter belongs. * @return The first month of the target quarter for the given month. */ public static int getQuarterFirstMonth(int firstMonthOfYear, int targetMonth) { int result = firstMonthOfYear; int index = firstMonthOfYear; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { if (index == targetMonth) return result; index = Month.nextIndex(index); } result = index; } throw new RuntimeException("Month not found : " + targetMonth); }
@Test public void nextMonthIndex() { assertEquals(2, Month.nextIndex(1)); assertEquals(3, Month.nextIndex(2)); assertEquals(4, Month.nextIndex(3)); assertEquals(5, Month.nextIndex(4)); assertEquals(6, Month.nextIndex(5)); assertEquals(7, Month.nextIndex(6)); assertEquals(8, Month.nextIndex(7)); assertEquals(9, Month.nextIndex(8)); assertEquals(10, Month.nextIndex(9)); assertEquals(11, Month.nextIndex(10)); assertEquals(12, Month.nextIndex(11)); assertEquals(1, Month.nextIndex(12)); }
@Test public void nextMonthIndex() { assertEquals(2, Month.nextIndex(1)); assertEquals(3, Month.nextIndex(2)); assertEquals(4, Month.nextIndex(3)); assertEquals(5, Month.nextIndex(4)); assertEquals(6, Month.nextIndex(5)); assertEquals(7, Month.nextIndex(6)); assertEquals(8, Month.nextIndex(7)); assertEquals(9, Month.nextIndex(8)); assertEquals(10, Month.nextIndex(9)); assertEquals(11, Month.nextIndex(10)); assertEquals(12, Month.nextIndex(11)); assertEquals(1, Month.nextIndex(12)); }
public IntervalListMonth(ColumnGroup columnGroup) { super(columnGroup); intervalMap = new HashMap<Integer, Interval>(); Month firstMonth = columnGroup.getFirstMonthOfYear(); int monthIndex = firstMonth.getIndex(); Month[] months = Month.getAll(); for (int i = 0; i < months.length; i++) { Month month = months[monthIndex-1]; Interval interval = new Interval(Integer.toString(month.getIndex()), i); interval.setType(columnGroup.getIntervalSize()); this.add(interval); intervalMap.put(monthIndex-1, interval); monthIndex = Month.nextIndex(monthIndex); } }
public IntervalListQuarter(ColumnGroup columnGroup) { super(columnGroup); intervalMap = new HashMap<Integer, Interval>(); Month firstMonth = columnGroup.getFirstMonthOfYear(); int monthIndex = firstMonth.getIndex(); for (int i = 0; i < 4; i++) { Quarter quarter = Quarter.getByIndex(i+1); Interval interval = new Interval((Integer.toString(quarter.getIndex())), i); interval.setType(columnGroup.getIntervalSize()); this.add(interval); for (int j = 0; j < 3; j++) { intervalMap.put(monthIndex-1, interval); monthIndex = Month.nextIndex(monthIndex); } } }