DateUtils { public static String ensureTwoDigits(String date, DateIntervalType intervalType) { String result = date; if (DateIntervalType.compare(intervalType, DateIntervalType.MONTH) <= 0) { result = ensureTwoDigits(result, MONTH_START, MONTH_END); } if (DateIntervalType.compare(intervalType, DateIntervalType.DAY) <= 0) { result = ensureTwoDigits(result, DAY_START, DAY_END); } if (DateIntervalType.compare(intervalType, DateIntervalType.HOUR) <= 0) { result = ensureTwoDigits(result, HOUR_START, HOUR_END); } if (DateIntervalType.compare(intervalType, DateIntervalType.MINUTE) <= 0) { result = ensureTwoDigits(result, MINUTE_START, MINUTE_END); } if (DateIntervalType.compare(intervalType, DateIntervalType.SECOND) <= 0) { result = ensureTwoDigits(result, SECOND_START, SECOND_END); } return result; }  static Date parseDate(DataColumn dateColumn, Object date); static Date parseDate(DateIntervalType type, GroupStrategy strategy, Object date); static String ensureTwoDigits(String date, DateIntervalType intervalType); static String ensureTwoDigits(String date, String symbolStart, String symbolEnd); static final SimpleDateFormat FORMATTER_YEAR; static final SimpleDateFormat FORMATTER_MONTH; static final SimpleDateFormat FORMATTER_DAY; static final SimpleDateFormat FORMATTER_HOUR; static final SimpleDateFormat FORMATTER_MINUTE; static final SimpleDateFormat FORMATTER_SECOND; }
@Test public void testMonthFormat() throws Exception { String result = DateUtils.ensureTwoDigits("2015-M1", DateIntervalType.MONTH); assertThat(result).isEqualTo("2015-01"); result = DateUtils.ensureTwoDigits("2015-M12", DateIntervalType.MONTH); assertThat(result).isEqualTo("2015-12"); }
@Test public void testDayFormat() throws Exception { String result = DateUtils.ensureTwoDigits("2015-M1-D23", DateIntervalType.DAY); assertThat(result).isEqualTo("2015-01-23"); result = DateUtils.ensureTwoDigits("2015-M11-D01", DateIntervalType.DAY); assertThat(result).isEqualTo("2015-11-01"); }
@Test public void testHourFormat() throws Exception { String result = DateUtils.ensureTwoDigits("2015-M1-D23 H3", DateIntervalType.HOUR); assertThat(result).isEqualTo("2015-01-23 03"); result = DateUtils.ensureTwoDigits("2015-M11-D01 H23", DateIntervalType.HOUR); assertThat(result).isEqualTo("2015-11-01 23"); }
@Test public void testMinuteFormat() throws Exception { String result = DateUtils.ensureTwoDigits("2015-M1-D23 H3:m0", DateIntervalType.MINUTE); assertThat(result).isEqualTo("2015-01-23 03:00"); result = DateUtils.ensureTwoDigits("2015-M11-D01 H23:m59", DateIntervalType.MINUTE); assertThat(result).isEqualTo("2015-11-01 23:59"); }
@Test public void testSecondsFormat() throws Exception { String result = DateUtils.ensureTwoDigits("2015-M1-D23 H3:m0:s0", DateIntervalType.SECOND); assertThat(result).isEqualTo("2015-01-23 03:00:00"); result = DateUtils.ensureTwoDigits("2015-M11-D01 H23:m59:s59", DateIntervalType.SECOND); assertThat(result).isEqualTo("2015-11-01 23:59:59"); }