如果直接返回时间,得到的是时间戳
@RequestMapping("/testJsonTime")
@ResponseBody
public Date testJsonTime(){
return new Date();
}

格式化返回
@RequestMapping("/testJsonTime")
@ResponseBody
public String testJsonTime() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//不使用时间戳的方式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//自定义日期格式对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//指定日期格式
mapper.setDateFormat(sdf);
return mapper.writeValueAsString(new Date());
}

本文介绍了一种在RESTful API中返回格式化日期的方法,通过配置ObjectMapper来避免发送时间戳,而是直接返回人类可读的日期格式。该方法适用于需要以特定格式返回日期的应用场景。
843

被折叠的 条评论
为什么被折叠?



