added Date / LocalDate conversion helper
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
package de.muehlencord.shared.util;
|
package de.muehlencord.shared.util;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -15,4 +18,18 @@ public class DateUtil {
|
|||||||
return Date.from(utc.toInstant());
|
return Date.from(utc.toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Date getDate(final LocalDate localDate) {
|
||||||
|
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LocalDate getDate(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.systemDefault());
|
||||||
|
return zdt.toLocalDate();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user