springboot returns the time format configuration mode of json format data
- 2021-12-11 07:18:44
- OfStack
Returns json Format Data Time Format Configuration
The time found out in the database is the wrong time format, and the previous section needs to be processed to show the corresponding format. If you turn it one by one, it is too troublesome, so you can add the following configuration in apllication. property
# Time stamp system 1 Conversion
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
time-zone is a time zone offset setting. If it is not specified, the time will be 8 hours different from Beijing time.
Returning json date format problem
The default date format returned in SpringBoot is similar to this:
"birth": 1537407384500
Or something like this:
"createTime": "2018-09-18T10:54:06.000+0000"
None of the above can meet the actual display needs
How to modify (only if the default jackson parsing package is used):
Modify the default format format in the application. properties/yml file:
spring.jackson.date-format=yyyy-MM-dd
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false
The above values of spring. jackson. date-format can be modified according to actual needs.
Then there will be a problem after modification: What if I want to return a different format? For example, yyyy-MM-dd or yyyy year, MM month, dd day, HH time, mm minutes and ss seconds
You can then set a default format in the above configuration file, and if you need another format, just add the following annotations to the fields in the relevant entity class:
@JsonFormat(pattern="yyyy Year MM Month dd Day HH Hour mm Points ss Seconds ",timezone = "GMT+8")
private Date registerDate;
At this time, the return format takes precedence over the format set by annotations. Flexibility can be achieved through the above methods.