added more date util functions

This commit is contained in:
2019-03-14 15:11:47 +01:00
parent 701f0dd3a0
commit 827b110e7c

View File

@ -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;
@ -27,8 +28,9 @@ public class DateUtil {
}
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();
@ -36,9 +38,17 @@ public class DateUtil {
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();
@ -46,4 +56,10 @@ public class DateUtil {
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();
}
}