当前位置:网站首页>@Differences between jsonformat and @datetimeformat
@Differences between jsonformat and @datetimeformat
2022-06-09 05:27:00 【Classmate Tao Ran.】
background
When getting time from database to front end for display , Sometimes we may not get a satisfactory time format of time date , The correct time format is displayed in the database , It turns out to be an ugly time stamp ,@JsonFormat Annotation solves this problem very well , We use @JsonFormat It can be solved very well : Back to front time format consistency problem , secondly , Another problem is , We are using WEB Time of service , May need to use , Pass in time to the backstage , For example, to register a new user, you need to fill in the date of birth , At this time, the time format passed from the foreground to the background is also inconsistent , And our counterpart has another annotation ,@DataTimeFormat So we can solve this problem very well , Next, record the specific @JsonFormat And DateTimeFormat Process of use .
Statement : About @JsonFormat Use , Be sure to import the correct complete package .
@JsonFormat
1. Use maven introduce @JsonFormat The required jar package , I'll post my pom File dependency
<!--JsonFormat-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>2. Add... To the attribute of the entity class corresponding to the database field of the time you need to query @JsonFormat
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class TestClass {
// Set the time zone to Shanghai time zone , The time format depends on your needs .
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
private Date testTime;
public Date gettestTime() {
return testTime;
}
public void settestTime(Date testTimee) {
this.testTime= testTime;
}
}Here's an explanation :@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
pattern: Is the format of the time and date you need to convert
timezone: It's time set to East 8 , Avoid time errors in conversion
Tips :@JsonFormat Annotations can be above properties , It can also be found in the property corresponding to get On the way , There is no difference between the two ways
3. After finishing the above two steps , We use the corresponding entity class to receive the results of the database query and complete the time format conversion , When returning to the front end, it will be in accordance with the time format we set
@DateTimeFormat
[email protected] The use of and @jsonFormat almost , The first thing to introduce is spring also jodatime,spring I won't
<!-- joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>2. stay controller Layer we use spring mvc When the form automatically encapsulates the mapped object , We add... To the corresponding property of the object receiving the foreground data @@DateTimeFormat
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symstarttime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symendtime;I just post these two attributes here , Here I use both annotations at the same time , Because I need to get the data to the front desk , It also needs the data from the front desk to the back desk , We need to change the time format , Can be used at the same time
3. After the above two steps , We can get a time format that conforms to the custom format and store it in the database
summary
annotation @JsonFormat Mainly from the background to the front of the time format conversion
annotation @DataFormAT It mainly refers to the transformation of time format from front to back
边栏推荐
- 材料之kube-dns.yaml
- pytorch DDP加速之gradient accumulation设置
- Lucene构建索引与执行搜索小记
- Remove duplicates from sort array -leetcode
- 基础集群部署 - kubernetes-simple
- Stack
- Alibaba cloud AI training camp - machine learning 2:xgboost
- Xtrabackup backup and recovery
- YOLOv5的Tricks | 【Trick6】学习率调整策略(One Cycle Policy、余弦退火等)
- Thinking of reading
猜你喜欢

2022 "Cyberspace Security" event module B of Jiangxi secondary vocational group - SQL injection test

Heap and priority queues

Design owlook network novel recommendation system

Why do I need a thread pool? What is pooling technology?

Requests segmented downloading of files and multi-threaded downloading

1- enter the database

Cloud computing technology

2022 welder (elementary) special operation certificate examination question bank and simulation examination

Windows uses php to start ThinkPHP project and deploy configuration

Product weekly report issue 28 | CSDN editor upgrade, adding the function of inserting existing videos
随机推荐
Do you know the ranking of hybrid cloud management platforms? Look here!
Rotate array leetcode
【IT】福昕pdf保持工具选择
Cuijian hasn't changed. BAIC Jihu should make a change
Intranet penetration hash delivery attack
reids 缓存与数据库数据不一致、缓存过期删除问题
Summary of Android Engineer interview experience with 5 years' work experience, summary of real interview questions of Ali + Tencent + byte jump
Li Kou today's question -1037 Effective boomerang
Pattern recognition big job PCA & Fisher & KNN & kmeans
Mining happiness with machine learning
Lighting - brightness attenuation of light
MySQL one master multi slave configuration centos7 pro test
Mysql5.7 dual master and dual slave configuration
A few minutes to understand the Flink waterline
Stack
Heap and priority queues
基础集群部署 - kubernetes-simple
Palindrome linked list leetcode
Alibaba cloud AI training camp MySQL foundation 1:
[C language] a quick pass operator