Add the following code to application.properties
Springboot uses jackson to parse json by default
spring.jackson.date-format=yyyy-MM-dd HH:mm:ssssspring.jackson.time-zone=GMT+8
If an individual entity needs to use pattern in other formats, add annotations to the entity
import org.springframework.format.annotation.DateTimeFormat;import com.fasterxml.jackson.annotation.JsonFormat;public class MrType { @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") @DateTimeFormat(pattern="yyyy-MM-dd") private Date createdDate;} Spring Boot date data format conversion @JsonFormat instance
There is usually Date type data in the pojo bean. The one that is returned directly through @ResponseBody is a long integer timestamp (the number of milliseconds from 1970 to the time of the variable). There are many reasons on the Internet, so I won't go into details here. If you want to return a custom date format, such as: yyyymmddhhmmss, you need to do related processing. There are many processing methods on the Internet, which are generally inherited and rewrite, which is quite complicated. In fact, JSON already has the annotation @JsonFormat support, use examples:
@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")private Date createTime;
Function: 1) When entering parameters, the request message only needs to be passed in the yyyymmddhhmmss string, and it will be automatically converted to Date type data. 2) When the parameter is issued, the Date type data is automatically converted into a 14-bit string and returned.
For details, please refer to: http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonFormat.html
Other related notes:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.