DateTimeUtil { public static DateTime floorToHour(DateTime value) { if (value == null) { return null; } return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), 0, 0, 0, value.getZone()); }  static DateTime toDateTime(Timestamp value); static DateTime copy(DateTime value); static Timestamp toTimestamp(DateTime dt); static DateTime now(); static DateTime parseEmbedded(String string0); static DateTime parseEmbedded(String string0, String pattern, DateTimeZone zone); static DateTime floorToYear(DateTime value); static DateTime floorToMonth(DateTime value); static DateTime floorToDay(DateTime value); static DateTime floorToHour(DateTime value); static DateTime floorToHalfHour(DateTime value); static DateTime floorToQuarterHour(DateTime value); static DateTime floorToTenMinutes(DateTime value); static DateTime floorToFiveMinutes(DateTime value); static DateTime floorToMinutePeriod(DateTime value, int periodInMinutes); static DateTime floorToMinute(DateTime value); static DateTime floorToSecond(DateTime value);  }
@Test public void floorToHour() throws Exception { DateTime dt0 = new DateTime(2009,6,24,23,30,30,789,DateTimeZone.forID("America/Los_Angeles")); DateTime dt1 = DateTimeUtil.floorToHour(dt0); Assert.assertEquals(2009, dt1.getYear()); Assert.assertEquals(6, dt1.getMonthOfYear()); Assert.assertEquals(24, dt1.getDayOfMonth()); Assert.assertEquals(23, dt1.getHourOfDay()); Assert.assertEquals(0, dt1.getMinuteOfHour()); Assert.assertEquals(0, dt1.getSecondOfMinute()); Assert.assertEquals(0, dt1.getMillisOfSecond()); Assert.assertEquals(DateTimeZone.forID("America/Los_Angeles"), dt1.getZone()); DateTime dt2 = DateTimeUtil.floorToHour(null); Assert.assertNull(dt2); }