In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to format the global date in Springboot. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Recently, several colleagues in the department left their jobs one after another after some grievances, and they were reluctant to leave when they worked together for three years. After all the formalities were done, they were sent out of the company after pleasantries, and several brothers smiled at me when they left. I immediately had a bad feeling in my heart that it was definitely not that simple. After I took over the project of these bosses, my hunch came true. Now I actually regret it a little bit. Why didn't I beat them up when I saw them off? Ha ~ and this impulse to hit people became stronger and stronger when I began to optimize the projects of several brothers.
There's a hole.
The technical department organizes code walking and optimization every month. In the past, they each reviewed and optimized their own projects, but now several brothers have left their projects and their projects fall on me. The most painful thing for programmers is to take over other people's projects and make optimizations, because this is not as simple as reconstructing a project. However, before the military order, there is no way to bear the scalp!
The first optimization point made me a little broken. These bosses have strong skills and have always been role models for me, but almost all date formatting in the project is done with SimpleDateFormat, like the following code. Emm~, how can injured men do anything? ha.
SvcOrderDailyStatisticsPo orderDailyStatisticsPo = new SvcOrderDailyStatisticsPo (); SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd"); Date stationTime = dateFormat.parse (dateFormat.format (svcWorkOrderPo.getPayEndTime (); orderDailyStatisticsPo.setStatisticalDate (stationTime)
And the time and date API in the project is also confused, considering that java.util.Date and java.util.Calendar do not support time zone, and non-thread safety, the calculation of the date is relatively cumbersome, the technical department unanimously requires the use of java.time LocalDateTime after JDK1.8. But a lot of people are still using java.util.Date and java.util.Calendar to process dates.
Optimization scheme
Time formatting is used very frequently, how to make time formatting simple and do not have to repeat the wheel, then it should be abstracted as a global date formatting, the following will briefly introduce several optimization schemes combined with practice.
Test address: http://127.0.0.1:8080/timeTest
@ GetMapping ("/ timeTest") public OrderInfo timeTest () {OrderInfo order = new OrderInfo (); order.setCreateTime (LocalDateTime.now ()); order.setUpdateTime (new Date ()); return order;} 1, @ JsonFormat comments
Using @ JsonFormat annotation formatting time should be regarded as a basic operation, and most developers apply this method, which is simple and convenient.
Author: xiaofu * @ Description: * / public class OrderInfo {@ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; @ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; public LocalDateTime getCreateTime () {return createTime } public void setCreateTime (LocalDateTime createTime) {this.createTime = createTime;} public Date getUpdateTime () {return updateTime;} public void setUpdateTime (Date updateTime) {this.updateTime = updateTime;}}
After testing the results, it is found that both the Date type and the LocalDateTime type are formatted successfully, but there is still a problem. It is still cumbersome to do so. The date field of each entity class has to be annotated with @ JsonFormat, which requires a lot of repetitive work. Then look down ~
2. Global configuration (1)
Springboot has provided us with date formatting ${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}. Here we need to configure it globally, which is relatively simple, and there is no need to add @ JsonFormat annotations to entity class attributes.
/ * * @ Author: xiaofu * @ Description: * / public class OrderInfo {/ / @ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; / / @ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; public LocalDateTime getCreateTime () {return createTime } public void setCreateTime (LocalDateTime createTime) {this.createTime = createTime;} public Date getUpdateTime () {return updateTime;} public void setUpdateTime (Date updateTime) {this.updateTime = updateTime;}}
You only need to define a configuration class with @ Configuration and inject two Bean to complete the global date formatting process, which is currently used in my project.
/ * * @ Author: xiaofu * @ Description: * / @ Configurationpublic class LocalDateTimeSerializerConfig {@ Value ("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; @ Bean public LocalDateTimeSerializer localDateTimeDeserializer () {return new LocalDateTimeSerializer (DateTimeFormatter.ofPattern (pattern));} @ Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer () {return builder-> builder.serializerByType (LocalDateTime.class, localDateTimeDeserializer ());}}
This method can support the coexistence of Date type and LocalDateTime type, so one problem is that the global time format is yyyy-MM-dd HH:mm:ss, but some fields need to be in yyyy-MM-dd format.
You need to use it with @ JsonFormat annotation and add @ JsonFormat annotation to specific field attributes, because @ JsonFormat annotation has a high priority and is mainly annotated in the time format of @ JsonFormat annotation.
3. Global configuration (2)
This global configuration is implemented in the same way as above, but note that after using this configuration, the field manual configuration @ JsonFormat annotation will no longer take effect.
/ * * @ Author: xiaofu * @ Description: * / public class OrderInfo {/ / @ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; / / @ JsonFormat (locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; public LocalDateTime getCreateTime () {return createTime } public void setCreateTime (LocalDateTime createTime) {this.createTime = createTime;} public Date getUpdateTime () {return updateTime;} public void setUpdateTime (Date updateTime) {this.updateTime = updateTime;}} / * * @ Author: xiaofu * @ Description: * / @ Configurationpublic class LocalDateTimeSerializerConfig {@ Value ("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern @ Bean @ Primary public ObjectMapper serializingObjectMapper () {ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule (); javaTimeModule.addSerializer (LocalDateTime.class, new LocalDateTimeSerializer ()); javaTimeModule.addDeserializer (LocalDateTime.class, new LocalDateTimeDeserializer ()); objectMapper.registerModule (javaTimeModule); return objectMapper } public class LocalDateTimeSerializer extends JsonSerializer {@ Override public void serialize (LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString (value.format (ofPattern (pattern);}} public class LocalDateTimeDeserializer extends JsonDeserializer {@ Override public LocalDateTime deserialize (JsonParser p, DeserializationContext deserializationContext) throws IOException {return LocalDateTime.parse (p.getValueAsString (), ofPattern (pattern)) } on how Springboot global date formatting is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.