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;
@ -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();
}
}