前台json日期转后台格式转换

后台格式化代码:

        

public class JsonDateSerializer extends JsonDeserializer<Date> {
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String date = jp.getText();
        try {
            if(date.length() == 19){
                return format.parse(date);//转时分秒
            }else if(date.length() == 10){
                return format1.parse(date);//转日期
            }else {
                return null;
            }

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
 
 

//实体类日期通过注解方式进行转换

//批准日期
@JsonDeserialize(using = JsonDateSerializer.class)
private Date prodZcrq;
//发证日期
@JsonDeserialize(using = JsonDateSerializer.class)
private Date fbzApprDate;
//批件到期日
@JsonDeserialize(using = JsonDateSerializer.class)
private Date prodPzjz;


猜你喜欢

转载自blog.csdn.net/u014450465/article/details/79383548
今日推荐