added more date util functions
This commit is contained in:
@ -2,6 +2,7 @@ package de.muehlencord.shared.util;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
@ -27,8 +28,9 @@ public class DateUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static LocalDate getDate(final Date date) {
|
public static LocalDate getDate(final Date date) {
|
||||||
if (date == null)
|
if (date == null) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
// ensure to call toInstant on java.util.Date and not on java.sql.Date because this will throw an UnsupportedOperationException
|
// 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()
|
// see Java doc of java.sql.Date.toInstant()
|
||||||
Instant instant = new java.util.Date(date.getTime()).toInstant();
|
Instant instant = new java.util.Date(date.getTime()).toInstant();
|
||||||
@ -36,9 +38,17 @@ public class DateUtil {
|
|||||||
return zdt.toLocalDate();
|
return zdt.toLocalDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LocalDate getUtcDate(final Date date) {
|
public static LocalDateTime getDateTime(final Date date) {
|
||||||
if (date == null)
|
if (date == null) {
|
||||||
return 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
|
// 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()
|
// see Java doc of java.sql.Date.toInstant()
|
||||||
Instant instant = new java.util.Date(date.getTime()).toInstant();
|
Instant instant = new java.util.Date(date.getTime()).toInstant();
|
||||||
@ -46,4 +56,10 @@ public class DateUtil {
|
|||||||
return zdt.toLocalDate();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user