DateTimeUtil { public static long roundDown(int field, long timeInMillis) { switch (field) { case Calendar.DAY_OF_MONTH: case Calendar.DAY_OF_WEEK: case Calendar.DAY_OF_YEAR: return (timeInMillis - timeInMillis % (24 * 60 * 60 * 1000)); case Calendar.HOUR: return (timeInMillis - timeInMillis % (60 * 60 * 1000)); case Calendar.MINUTE: return (timeInMillis - timeInMillis % (60 * 1000)); case Calendar.SECOND: return (timeInMillis - timeInMillis % (1000)); default: return 0L; } }  static Date humanDateToDate(String date); static long getCurrentTimestamp(); static String secondsToHumanDate(long seconds); static String secondsToHumanDate(long seconds, TimeZone timeZone); static String millisecondsToHumanDateWithMilliseconds(long milliseconds); static String millisecondsToHumanDateWithSeconds(long milliseconds); static String millisecondsToHumanDateWithSecondsAndTimezone(long milliseconds); static long humanDateToSeconds(String date); static long humanDateToSeconds(String date, TimeZone timeZone); static long humanDateToMilliseconds(String date); static long humanDateToMilliseconds(String date, TimeZone timeZone); static long humanDateToMillisecondsWithoutException(String date); static long humanDateToSecondsWithoutException(String date); static long roundDown(int field, long timeInMillis); static String getCalendarFieldName(int field); static String format(long milliseconds, String format); static long maprhumanDateToMilliseconds(String date); static long parseTimeStrToMilliseconds(String timeStr); static final long ONESECOND; static final long ONEMINUTE; static final long ONEHOUR; static final long ONEDAY; static TimeZone CURRENT_TIME_ZONE; }
@Test public void testRound1() { long tsInMS = 1397016731576L; long tsInMin = DateTimeUtil.roundDown(Calendar.MINUTE, tsInMS); Assert.assertEquals(1397016720000L, tsInMin); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis(tsInMS); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Assert.assertEquals(tsInMin, cal.getTimeInMillis()); }
@Test public void testRound2() { long tsInMS = 1397016731576L; long tsInHour = DateTimeUtil.roundDown(Calendar.HOUR, tsInMS); Assert.assertEquals(1397016000000L, tsInHour); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis(tsInMS); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Assert.assertEquals(tsInHour, cal.getTimeInMillis()); }
@Test public void testRound3() { long tsInMS = 1L; long tsInDay = DateTimeUtil.roundDown(Calendar.DATE, tsInMS); Assert.assertEquals(0L, tsInDay); }