// 日期问题
public String json5() throws Exception {
ObjectMapper mapper = new ObjectMapper();
// 1.如何让他不返回时间戳!所以我们要关闭时间戳功能
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 2.时间格式化问题!自定义日期格式对象
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 3.让mapper指定时间日期格式为
mapper.setDateFormat(s);
// 写一个日期对象
Date date = new Date();
return mapper.writeValueAsString(date);
}
或者使用工具类完成
public class JsonUtil {
public static String getJson(Object object) {
return getJson(object, “yyyy-MM-dd HH:mm:ss”);
}
public static String getJson(Object o, String dateFormat) {
ObjectMapper mapper = new ObjectMapper();
// 1.如何让他不返回时间戳!所以我们要关闭时间戳功能
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 2.时间格式化问题!自定义日期格式对象
SimpleDateFormat s = new SimpleDateFormat(dateFormat);
// 3.让mapper指定时间日期格式为
mapper.setDateFormat(s);
// 写一个日期对象
Date date = new Date();
try {
return mapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}