当前位置:网站首页>@JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
@JsonFormat(pattern=“yyyy-MM-dd“)时间差问题
2022-07-31 22:11:00 【华为云】
最近在做给一个同学做课程设计项目的时候,需要Java后端给前端页面返回时间、比如考试时间这种、前端用的vue+element时间选择器、(要用本地时间–北京时间),后端时间字段映射,转换,但数据传到后台就会比北京时间少8个小时。具体检查了下是因为时区的问题。但前端显示还是少8个小时,这样的话那肯定还是有问题的。可以利用@JsonFormat绑定到对象属性上解决问题。
原因:@JsonFormat(pattern=“yyyy-MM-dd”)时间差问题
解释:默认情况下timeZone为GMT(即标准时区),而北京是在东八区,所以会造成差8小时。
解决方法:加上timezone = "GMT+8
1 @JsonFormat(pattern=“yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”)
2 也可以在Springboot application.yml中修改MySQL配置
druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/renren_students?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456扩展知识:
注解@JsonFormat的使用
在项目maven引入@JsonFormat所需要的jar包。主要如下
<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>@JsonFormat注解是一个时间格式化注解,比如我们存储在mysql中的数据是date类型的,当我们读取出来封装在实体类中的时候,就会变成英文时间格式,而不是yyyy-MM-dd HH:mm:ss这样的中文时间,因此我们需要用到JsonFormat注解来格式化我们的时间。一般用于一个字段命名上面在对应的查询数据库对象字段对应的实体类的属性上添加@JsonFormat属性。//设置时区为上海时区,时间格式自己据需求定。比如:@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)。
大家点赞、收藏、关注、评论啦 、
边栏推荐
- 嵌入式开发没有激情了,正常吗?
- How to identify fake reptiles?
- Socket回顾与I/0模型
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
- Golang - from entry to abandonment
- "APIO2010" Patrol Problem Solution
- 角色妆容的实现
- Flex layout in detail
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- uniapp小程序检查、提示更新
猜你喜欢

Pytest first experience

GateWay implements load balancing

Realization of character makeup

Summary of the classic drawing method of histogram

VOT2021比赛简介

如何减少软件设计和实现之间鸿沟

Financial profitability and solvency indicators

How to debug TestCafe

嵌入式开发没有激情了,正常吗?

21. Support Vector Machine - Introduction to Kernel Functions
随机推荐
IDA PRO中汇编结构体识别
Recognize anomalies (you will understand after reading this)
Verilog implements a divide-by-9 with a duty cycle of 5/18
Quick Start Tutorial for flyway
"APIO2010" Patrol Problem Solution
Mobile web development 02
Structure of the actual combat battalion module eight operations
Learn about C# anonymous methods
Short-circuit characteristics and protection of SiC MOSFETs
Collation of knowledge points in Ningbo University NBU IT project management final exam
信息学奥赛一本通 1941:【07NOIP普及组】Hanoi双塔问题 | 洛谷 P1096 [NOIP2007 普及组] Hanoi 双塔问题
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
【愚公系列】2022年07月 Go教学课程 025-递归函数
Summary of the classic drawing method of histogram
Count characters in UTF-8 string function
Socket回顾与I/0模型
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
UserAgent resolution
cas and spin locks (is lightweight locks spin locks)
嵌入式开发没有激情了,正常吗?