com.hframework.common.springext.converter.DateJsonSerializer Maven / Gradle / Ivy
package com.hframework.common.springext.converter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by zhangquanhong on 2016/6/22.
*/
public class DateJsonSerializer extends JsonSerializer {
public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
if(date == null) {
jsonGenerator.writeString("");
return ;
}
jsonGenerator.writeString(format.format(date));
}
}