From 827b110e7c527c381cab48c9be82b9ec63008bc0 Mon Sep 17 00:00:00 2001 From: jomu Date: Thu, 14 Mar 2019 15:11:47 +0100 Subject: [PATCH] added more date util functions --- .../de/muehlencord/shared/util/DateUtil.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/util/src/main/java/de/muehlencord/shared/util/DateUtil.java b/util/src/main/java/de/muehlencord/shared/util/DateUtil.java index b0563ee..1b27a81 100644 --- a/util/src/main/java/de/muehlencord/shared/util/DateUtil.java +++ b/util/src/main/java/de/muehlencord/shared/util/DateUtil.java @@ -2,6 +2,7 @@ package de.muehlencord.shared.util; import java.time.Instant; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -21,29 +22,44 @@ public class DateUtil { public static Date getDate(final LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); } - + public static Date getUtcDate(final LocalDate localDate) { return Date.from(localDate.atStartOfDay(ZoneId.of("UTC")).toInstant()); - } + } public static LocalDate getDate(final Date date) { - if (date == null) + if (date == null) { return null; + } // ensure to call toInstant on java.util.Date and not on java.sql.Date because this will throw an UnsupportedOperationException // see Java doc of java.sql.Date.toInstant() Instant instant = new java.util.Date(date.getTime()).toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); return zdt.toLocalDate(); } - - public static LocalDate getUtcDate(final Date date) { - if (date == null) + + public static LocalDateTime getDateTime(final Date date) { + if (date == null) { return null; + } + return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); + } + + public static LocalDate getUtcDate(final Date date) { + if (date == null) { + return null; + } // ensure to call toInstant on java.util.Date and not on java.sql.Date because this will throw an UnsupportedOperationException // see Java doc of java.sql.Date.toInstant() Instant instant = new java.util.Date(date.getTime()).toInstant(); ZonedDateTime zdt = instant.atZone(ZoneId.of("UTC")); return zdt.toLocalDate(); - } + } + public static LocalDateTime getUtcDateTime(final Date date) { + if (date == null) { + return null; + } + return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.of("UTC")).toLocalDateTime(); + } }