Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Springboot returns data in json format. How is the time format configured?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Springboot return json format data time format configuration is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Returns json format data time format configuration

The time found in the database is in the wrong format, and the previous paragraph needs to be processed to show the corresponding format. It is too troublesome to transfer one by one, so you can add the following configuration to apllication.property.

# timestamp uniform conversion spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.time-zone=GMT+8

Where time-zone is the time zone offset setting, if not specified, there will be a difference of eight hours between Beijing time and Beijing time.

Returns the JSON date format question

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.

Modification method (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-ddspring.jackson.time-zone=GMT+8spring.jackson.serialization.write-dates-as-timestamps=false

The value of the spring.jackson.date-format above can be modified according to the actual needs.

Then there will be a problem after modification: what if I want to return to a different format? For example, yyyy-MM-dd or yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Then you can set a default format in the configuration file above, and then if you need other formats, you only need to add the following comments to the fields in the relevant entity class:

@ JsonFormat (pattern= "mm minutes ss seconds at HH, month ddday, yyyy year", timezone = "GMT+8") private Date registerDate

At this point, the return format takes precedence with the format set by the annotation. Through the above ways, we can be flexible.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report