DateTimeUtils { public synchronized static Date getHoursLaterDate(int hours) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.HOUR_OF_DAY, hours); 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 testGetHoursLaterDate1() throws Exception { Date now = new Date(); Date date = DateTimeUtils.getHoursLaterDate(1); Assert.assertTrue(now.before(date)); long duration = date.getTime() - now.getTime(); TimeUnit.HOURS.toMillis(duration); Assert.assertTrue(Math.abs(TimeUnit.HOURS.toMillis(1) - duration) < TimeUnit.SECONDS.toMillis(1)); }
@Test public void testGetHoursLaterDate2() throws Exception { Date now = new Date(); Date before = DateTimeUtils.getHoursLaterDate(-1); Assert.assertTrue(now.after(before)); long duration = now.getTime() - before.getTime(); Assert.assertTrue(Math.abs(TimeUnit.HOURS.toMillis(1) - duration) < TimeUnit.SECONDS.toMillis(1)); }