项目使用的数据库是mysql 持久层框架是hibernate 利用SQLQuery 获取日期的时候缺少时分秒 将 .addScalar("dealTS",StandardBasicTypes.DATE)
改成 .addScalar("dealTS",StandardBasicTypes.TIMESTAMP),还有在模型的日期属性get set方法 上加上注解@JsonSerialize(using = CustomJsonDateSerializer.class)
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
CustomJsonDateSerializer 和 CustomJsonDateDeserializer 是自己写的继承JsonSerializer
public class CustomJsonDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date aDate, JsonGenerator aJsonGenerator, SerializerProvider aSerializerProvider)
throws IOException, JsonProcessingException {
SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.ISO8601_FORMAT);//"yyyy-MM-dd'T'HH:mm:ss"
String dateString = dateFormat.format(aDate);
aJsonGenerator.writeString(dateString);
}
}
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jsonparser,
DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat(Constants.ISO8601_FORMAT);//"yyyy-MM-dd'T'HH:mm:ss"
String date = jsonparser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}