PackedLocalTime { public static int truncatedTo(TemporalUnit unit, int packedTime) { if (unit == ChronoUnit.NANOS || unit == ChronoUnit.MILLIS) { return packedTime; } Duration unitDur = unit.getDuration(); if (unitDur.getSeconds() > SECONDS_PER_DAY) { throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation"); } int hour = PackedLocalTime.getHour(packedTime); int minute = PackedLocalTime.getMinute(packedTime); int second = PackedLocalTime.getSecond(packedTime); int milli = 0; if (unit == ChronoUnit.DAYS) { hour = 0; minute = 0; second = 0; } else if (unit == ChronoUnit.HALF_DAYS) { if (hour >= 12) { hour = 12; } else { hour = 0; } minute = 0; second = 0; } else if (unit == ChronoUnit.HOURS) { minute = 0; second = 0; } else if (unit == ChronoUnit.MINUTES) { second = 0; } return PackedLocalTime.create(hour, minute, second, milli); }  static byte getHour(int time); static int of(int hour, int minute); static int of(int hour, int minute, int second); static int of(int hour, int minute, int second, int millis); static int truncatedTo(TemporalUnit unit, int packedTime); static int plusHours(int hoursToAdd, int packedTime); static int plusMinutes(int minutesToAdd, int packedTime); static int plusSeconds(int secondsToAdd, int packedTime); static int plusMilliseconds(int msToAdd, int packedTime); static int minusHours(int hoursToSubtract, int packedTime); static int minusMinutes(int minutesToSubtract, int packedTime); static int minusSeconds(int secondsToSubtract, int packedTime); static int minusMilliseconds(int millisToSubtract, int packedTime); static int withHour(int hour, int packedTime); static int withMinute(int minute, int packedTime); static int withSecond(int second, int packedTime); static int withMillisecond(int milliseconds, int packedTime); static char getMillisecondOfMinute(int time); static int getNano(int time); static int getMilliseconds(int time); static long toNanoOfDay(int time); static LocalTime asLocalTime(int time); static byte getMinute(int time); static int pack(LocalTime time); static byte getSecond(int packedLocalTime); static int getMinuteOfDay(int packedLocalTime); static int getSecondOfDay(int packedLocalTime); static int getMillisecondOfDay(int packedLocalTime); static String toShortTimeString(int time); static boolean isMidnight(int packedTime); static boolean isNoon(int packedTime); static boolean isAfter(int packedTime, int otherPackedTime); static boolean isOnOrAfter(int packedTime, int otherPackedTime); static boolean isBefore(int packedTime, int otherPackedTime); static boolean isOnOrBefore(int packedTime, int otherPackedTime); static boolean isEqualTo(int packedTime, int otherPackedTime); static boolean AM(int packedTime); static boolean PM(int packedTime); static int hoursUntil(int packedTimeEnd, int packedTimeStart); static int minutesUntil(int packedTimeEnd, int packedTimeStart); static int secondsUntil(int packedTimeEnd, int packedTimeStart);  }
@Test public void testTruncatedTo() { List<LocalTime> times = ImmutableList.of( LocalTime.of(5, 11, 24), LocalTime.of(21, 11, 24), LocalTime.MIDNIGHT, LocalTime.NOON, LocalTime.MIN, LocalTime.MAX); for (LocalTime time : times) { assertEquals(time.truncatedTo(SECONDS), asLocalTime(truncatedTo(SECONDS, pack(time)))); assertEquals(time.truncatedTo(MINUTES), asLocalTime(truncatedTo(MINUTES, pack(time)))); assertEquals(time.truncatedTo(HOURS), asLocalTime(truncatedTo(HOURS, pack(time)))); assertEquals(time.truncatedTo(HALF_DAYS), asLocalTime(truncatedTo(HALF_DAYS, pack(time)))); assertEquals(time.truncatedTo(DAYS), asLocalTime(truncatedTo(DAYS, pack(time)))); } }