springboot中日期格式转换问题
参考:https://blog.****.net/duanleiyadang/article/details/79883144
实体类中代表日期的字段是Date类型:
数据库中代表日期的也是:
但是查询出来的时间是这种形式的:2019-04-29T00:16:00.000+0000
我用的fastjson:
处理日期格式:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpOutputMessage; import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.IOException; import java.io.OutputStream; import java.util.Date; /** * @description: * @author: Administrator * @date: 2019-04-29 13:34 */ @Configuration public class FastJsonHttpDateConverter extends FastJsonHttpMessageConverter { private static SerializeConfig mapping = new SerializeConfig(); private static String dateFormat; static { dateFormat = "yyyy-MM-dd HH:mm:ss"; mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat)); } @Override protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { // TODO Auto-generated method stub OutputStream out = outputMessage.getBody(); String text = JSON.toJSONString(obj, mapping, this.getFeatures()); byte[] bytes = text.getBytes(this.getCharset()); out.write(bytes); } }
查询后会变成:
yyyy-MM-dd HH:mm:ss这种格式的日期数据 :2019-04-29 08:16:00