All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.pagar.util.LocalDateAdapter Maven / Gradle / Ivy

The newest version!
package me.pagar.util;

import com.google.common.base.Strings;
import com.google.gson.*;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormat;

import java.lang.reflect.Type;

public class LocalDateAdapter implements JsonDeserializer, JsonSerializer {

    private final DateTimeFormatter formatter;

    public LocalDateAdapter() {
        this.formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    }

    public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        final String dateTime = json.getAsString();
        return Strings.isNullOrEmpty(dateTime) ? null : formatter.parseLocalDate(dateTime);
    }

    public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
        return src == null ? null : new JsonPrimitive(formatter.print(src));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy