DateUtil { public static Instant snapToSeconds(Instant date, int seconds) { notNull(date, "date can't be null"); Instant instantSnapToSeconds = truncatedToSeconds(date); long remainder = instantSnapToSeconds.getEpochSecond() % seconds; return truncatedToSeconds(date.minusSeconds(remainder)); }  static Instant truncatedToDay(Instant date); static Instant truncatedToWeek(Instant date); static Instant snapToSeconds(Instant date, int seconds); static Instant truncatedToSeconds(Instant date); static String epochSecondToString(long metricTimestamp); static Instant epochSecondToInstant(long metricTimestamp); static boolean isBetweenHours(int hour, int startHour, int endHour); static boolean crossesMidnight(int startHour, int endHour); static ZonedDateTime instantToUTCDateTime(Instant epochSecond); static final int MIDNIGHT_HOUR; }
@Test public void testSnapToSeconds() { doTestSnappedToSeconds("2018-04-01T01:09:55Z", "2018-04-01T01:05:00Z", 300); doTestSnappedToSeconds("2018-04-01T01:07:15Z", "2018-04-01T01:05:00Z", 300); doTestSnappedToSeconds("2018-04-01T01:05:51Z", "2018-04-01T01:05:00Z", 300); doTestSnappedToSeconds("2018-04-01T01:07:55Z", "2018-04-01T01:07:00Z", 60); doTestSnappedToSeconds("2018-04-01T01:07:05Z", "2018-04-01T01:07:00Z", 60); doTestSnappedToSeconds("2018-04-01T01:07:30Z", "2018-04-01T01:07:00Z", 60); doTestSnappedToSeconds("2018-04-01T01:07:55Z", "2018-04-01T01:07:30Z", 30); doTestSnappedToSeconds("2018-04-01T01:07:35Z", "2018-04-01T01:07:30Z", 30); doTestSnappedToSeconds("2018-04-01T01:07:05Z", "2018-04-01T01:07:00Z", 30); }