第一种:使用java的SimpleDateformat
@RequestMapping(value = "/t1")
public String test2() throws JsonProcessingException {
// 默认会返回时间戳形式,有两种转换方式
Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
// 第一种 java SimpleDateFormat
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
return new ObjectMapper().writeValueAsString(format);
}
第二种:使用Jackson中的方法
@RequestMapping(value = "/t2")
public String test2() throws JsonProcessingException {
// 默认会返回时间戳形式,有两种转换方式
Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
// 第二种 使用jackson中的方法
ObjectMapper om = new ObjectMapper();
// 禁止使用时间戳
om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 自定义日期的格式
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return om.writeValueAsString(time);
}