封装格式转换工具类
将格式转换成 yyyy-MM-dd 格式
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.SerializerProvider; import org.springframework.stereotype.Component; @Component public class JsonDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(value); jgen.writeString(formattedDate); } }
将格式转换成 yyyy-MM-dd HH:mm:ss 格式
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.SerializerProvider; import org.springframework.stereotype.Component; @Component public class JsonTimeSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = formatter.format(value); jgen.writeString(formattedDate); } }
在封装dto数据实体的时候进行调用 将返回的时间转换成yyyy-MM-dd 格式
/** * 描述:需求日期 * 字段: need_date DATE(10) */ private java.util.Date needDate; @JsonSerialize(using = JsonDateSerializer.class) public java.util.Date getNeedDate() { return needDate; } public void setNeedDate(java.util.Date needDate) { this.needDate = needDate; }
时间: 2024-10-09 19:22:55