日期时间转换JSON
作者:互联网
导入json依赖
<!--json依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
关键代码
package com.xx.mvc.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author xx
* @version 1.0
*/
@Controller
public class JSONController {
@RequestMapping(value = "/dateToJson", produces = "application/json;charset=utf-8")
@ResponseBody
public String dateToJson() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//关闭它的时间戳功能
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//时间格式化:自定义日期格式对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//让mapper指定时间日期格式为simpleDateFormat
mapper.setDateFormat(simpleDateFormat);
//创建一个日期对象
Date date = new Date();
return mapper.writeValueAsString(date);
}
}
标签:mapper,jackson,日期,databind,JSON,import,fasterxml,com,转换 来源: https://www.cnblogs.com/lyycode/p/15237658.html