问题描述:
oracle 10G中用户表有一个字段是日期型,数据格式为yyyy-MM-dd HH:mm:ss,前端显示时只能显示成yyyy-MM-dd 后面的 HH:mm:ss不显示。
经过一番痛苦的原因分析,发现应该将用户实体的日期类型由java.sql.Date修改成java.util.Date型,并且对 jackson进行如下处理:
private void Test(MyUser user)
{
ObjectMapper mapper = new ObjectMapper();
Writer strWriter = new StringWriter();
try {
// mapper.writeValue(strWriter, user);
System.out.println("时间");
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d;
try {
d = (Date) df.parse("2011-10-12 12:12:12");
user.setCreatedatetime(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(user.getCreatedatetime());
// DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SerializationConfig serConfig = mapper.getSerializationConfig();
serConfig.setDateFormat(dateFormat);
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
deserializationConfig.setDateFormat(dateFormat);
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.writeValue(strWriter, user);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String userDataJSON = strWriter.toString();
System.out.println(userDataJSON);
}
输出为:
时间
Wed Oct 12 12:12:12 CST 2011,默认实体类日期输出格式
{"id":262,"username":"zhaohaiyan","password":"2","createdatetime":"2011-10-12 12:12:12","modifydatetime":"2014-05-15 09:17:05"},经过格式化后显示。