StatisticTimeUtils { public static Date getStatisticTime(final StatisticInterval interval, final int offset) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); switch (interval) { case DAY: calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.add(Calendar.DATE, offset); break; case HOUR: calendar.set(Calendar.MINUTE, 0); calendar.add(Calendar.HOUR_OF_DAY, offset); break; case MINUTE: default: calendar.add(Calendar.MINUTE, offset); break; } return calendar.getTime(); }  static Date getCurrentStatisticTime(final StatisticInterval interval); static Date getStatisticTime(final StatisticInterval interval, final int offset);  }
@Test public void assertGetStatisticTime() { assertThat(getTimeStr(StatisticTimeUtils.getStatisticTime(StatisticInterval.MINUTE, -1), StatisticInterval.MINUTE), is(getTimeStr(getLastMinute(), StatisticInterval.MINUTE))); assertThat(getTimeStr(StatisticTimeUtils.getStatisticTime(StatisticInterval.HOUR, -1), StatisticInterval.HOUR), is(getTimeStr(getLastHour(), StatisticInterval.HOUR))); assertThat(getTimeStr(StatisticTimeUtils.getStatisticTime(StatisticInterval.DAY, -1), StatisticInterval.DAY), is(getTimeStr(getYesterday(), StatisticInterval.DAY))); }