当前位置:网站首页>@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”)。
大家点赞、收藏、关注、评论啦 、
边栏推荐
- Mobile web development 02
- BM3 flips the nodes in the linked list in groups of k
- [Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
- grep命令 笔试题
- 【论文精读】iNeRF
- The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11
- Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
- 二叉树非递归遍历
- [Intensive reading of the paper] iNeRF
- Binary tree non-recursive traversal
猜你喜欢
如何减少软件设计和实现之间鸿沟
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
Flex layout in detail
Pytest初体验
IDA PRO中汇编结构体识别
全网一触即发,自媒体人的内容分发全能助手——融媒宝
[NLP] What is the memory of the model!
Write a database document management tool based on WPF repeating the wheel (1)
TestCafeSummary
高效并发:Synchornized的锁优化详解
随机推荐
Several methods for deleting specified elements in Golang slices
Shell script quick start to actual combat -02
【AcWing】The 62nd Weekly Match 【2022.07.30】
Returns a zero-length array or empty collection, do not return null
NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
顺序表的实现
BM3 将链表中的节点每k个一组翻转
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
二叉树非递归遍历
Chapter Six
Bionic caterpillar robot source code
MATLAB program design and application 2.4 Common internal functions of MATLAB
了解下C# 匿名方法
linux view redis version command (linux view mysql version number)
renderjs usage in uni-app
IDA PRO中汇编结构体识别
Book of the Month (202207): The Definitive Guide to Swift Programming
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Thymeleaf是什么?该如何使用。
useragent online lookup