public static final Gson gson = new GsonBuilder() .serializeNulls() .registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (localDateTime, typeOfSrc, context) -> { if (Objects.isNull(localDateTime)) { return new JsonPrimitive(""); } return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER))); }) .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json1, typeOfT, context) -> { String string = json1.getAsJsonPrimitive().getAsString(); if (Strings.isNullOrEmpty(string)) { return null; } try { return LocalDateTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER)); }catch (RuntimeException exception){ return LocalDateTime.parse(string, DateTimeFormatter.ISO_DATE_TIME); } }) .registerTypeAdapter(LocalDate.class, (JsonSerializer<LocalDate>) (localDate, type, jsonSerializationContext) -> { if (Objects.isNull(localDate)) { return new JsonPrimitive(""); } return new JsonPrimitive(localDate.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER))); }) .registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDate>) (json, typeOfT, context) -> { String string = json.getAsJsonPrimitive().getAsString(); if (Strings.isNullOrEmpty(string)) { return null; } return LocalDate.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER)); }) .registerTypeAdapter(LocalTime.class, (JsonSerializer<LocalTime>) (localTime, type, jsonSerializationContext) -> { if (Objects.isNull(localTime)) { return new JsonPrimitive(""); } return new JsonPrimitive(localTime.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER))); }) .registerTypeAdapter(LocalTime.class, (JsonDeserializer<LocalTime>) (json, typeOfT, context) -> { String string = json.getAsJsonPrimitive().getAsString(); if (Strings.isNullOrEmpty(string)) { return null; } return LocalTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER)); }) .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) -> { if (Objects.isNull(date)) { return new JsonPrimitive(""); } SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMATTER); //SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); //fmt.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); return new JsonPrimitive(fmt.format(date)); }) .create();