From e4a4f742a5c70e7886fb53d76c0e3e44797b52dd Mon Sep 17 00:00:00 2001 From: Sébastien Lesaint Date: Wed, 20 Sep 2017 10:16:36 +0200 Subject: add DateUtils#truncateToSeconds and use it instead of +1_000L --- .../src/main/java/org/sonar/api/utils/DateUtils.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'sonar-plugin-api') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java index ea232916471..01e93aafa03 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java @@ -285,4 +285,22 @@ public final class DateUtils { return Date.from(date.toInstant().plus(numberOfDays, ChronoUnit.DAYS)); } + @CheckForNull + public static Date truncateToSeconds(@Nullable Date d) { + if (d == null) { + return null; + } + return truncateToSecondsImpl(d); + } + + public static long truncateToSeconds(long dateTime) { + return truncateToSecondsImpl(new Date(dateTime)).getTime(); + } + + private static Date truncateToSecondsImpl(Date d) { + Instant instant = d.toInstant(); + instant = instant.truncatedTo(ChronoUnit.SECONDS); + return Date.from(instant); + } + } -- cgit v1.2.3