27 lines
826 B
Java
27 lines
826 B
Java
package de.ph87.home.common;
|
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
|
|
import java.io.IOException;
|
|
import java.time.Instant;
|
|
import java.time.ZoneId;
|
|
import java.time.ZonedDateTime;
|
|
|
|
public class EpochSecondsToZonedDateTime extends JsonDeserializer<ZonedDateTime> {
|
|
|
|
@Override
|
|
public ZonedDateTime deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
|
|
final long epochSeconds = jsonParser.getLongValue();
|
|
if (epochSeconds < 0) {
|
|
throw new RuntimeException();
|
|
}
|
|
if (epochSeconds == 0) {
|
|
return null;
|
|
}
|
|
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds), ZoneId.systemDefault());
|
|
}
|
|
|
|
}
|