added getUtcDate methods

This commit is contained in:
2019-01-25 17:17:29 +01:00
parent 4daa43ad04
commit 9f301136f0

View File

@ -21,6 +21,10 @@ public class DateUtil {
public static Date getDate(final LocalDate localDate) { public static Date getDate(final LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); 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) { public static LocalDate getDate(final Date date) {
if (date == null) if (date == null)
@ -31,5 +35,15 @@ public class DateUtil {
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
return zdt.toLocalDate(); return zdt.toLocalDate();
} }
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();
}
} }