package com.flightfeather.uav.common.utils; import com.google.gson.*; import java.lang.reflect.Type; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * LocalDateTime类型的时间格式序列化和反序列化类 * by hc 2024.12.6 */ public class LocalDateTimeAdapter implements JsonDeserializer, JsonSerializer { private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(dateTimeFormatter.format(src)); } @Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return LocalDateTime.parse(json.getAsString(), dateTimeFormatter); } catch (Exception e) { throw new JsonParseException(e); } } }