Java 8 LocalDateTime deserialized using Gson

前端 未结 4 874
無奈伤痛
無奈伤痛 2020-12-01 14:04

I have JSONs with a date-time attribute in the format \"2014-03-10T18:46:40.000Z\", which I want to deserialize into a java.time.LocalDateTime field using Gson.

When

4条回答
  •  心在旅途
    2020-12-01 14:31

    To even further extend @Evers answer:

    You can further simplify with a lambda like so:

    GSON GSON = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer) (json, type, jsonDeserializationContext) ->
        ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString()).toLocalDateTime()).create();
    

提交回复
热议问题