JsonDateDeserializer.java /** * Example: * import com.fasterxml.jackson.databind.annotation.JsonDeserialize; * import java.util.Date; * * @JsonDeserialize(using = JsonDateDeserializer.class) * private Date date; * /import com.fasterxml.jack
/** * Example: * import com.fasterxml.jackson.databind.annotation.JsonDeserialize; * import java.util.Date; * * @JsonDeserialize(using = JsonDateDeserializer.class) * private Date date; * / import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class JsonDateDeserializer extends JsonDeserializer{ @Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 指定接收的日期的字符串的格式 String date = jp.getText(); try { return format.parse(date); } catch (ParseException e) { throw new RuntimeException(e); } } }