DateTimeUtils { public static Date getHoursBeforeDate(Date date, int hours) { int minusHours = -Math.abs(hours); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.HOUR_OF_DAY, minusHours); return cal.getTime(); }  static String currentTimeAsString(); static String timeAsString(Date date); static String timeAsString(long timeMilli); synchronized static Date getHoursLaterDate(int hours); synchronized static Date getMinutesLaterThan(Date date, int minute); synchronized static Date getSecondsLaterThan(Date date, int second); static Date getHoursBeforeDate(Date date, int hours); static Date getNearestHour(); static String format; }
@Test public void testGetHoursBeforeDate() throws Exception { Date now = new Date(); Date before = DateTimeUtils.getHoursBeforeDate(now, 1); long duration = now.getTime() - before.getTime(); Assert.assertTrue((Math.abs(TimeUnit.HOURS.toMillis(1L) - duration) < TimeUnit.SECONDS.toMillis(1))); }