当前位置:网站首页>MongoDB数据日期显示相差8小时 原因和解决方案
MongoDB数据日期显示相差8小时 原因和解决方案
2022-07-04 09:36:00 【gblfy】
一、透过现象看本质
1. 背景调研
最近因为项目需要使用到了MongoDB,使用Navicat Premium 15 客户端可视化工具查询数据,可是问题来了,插入的数据在查看的时候显示相差了8个小时,但是程序里是通过new Date() 来生成时间的,系统时间确认没问题,经过多次测试发现程序调用得到的日期也没有问题,那么只能是客户端程序的显示问题了。
- 正常时间为:
2022-07-03 12:38:11
- 实际落库时间
2022-07-03 04:38:11
2. 原因分析
时区差异:
存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00
说明:UTC 即 Universal Time Coordinated,即0时区与东八区相差8小时。
3. 影响评估
1.数据库查询数据->显示问题
2.查询数据不正确->页面返回数据也少了8小时
二、解决方案
2.1. 客户端显示问题
下载可视化工具Robo3T
打开Robo3T;
- 点击Options -> display dates in…可以看到有2个选项:
- 1.UTC(默认)
- 2.Local TimeZone.
- 选择Local TimeZone,再次查看数据时间,发现已经正常。
注:UTC 即 Universal Time Coordinated,即0时区与东八区相差8小时。
2.2. 查询数据不正确
在返回的对象属性上添加@JsonFormat注解和配置
@JsonFormat(locale = "zh", timezone = "Asia/Shanghai", pattern = "yyyy-MM-dd HH:mm:ss")
@Field("createTime")
private Date createTime; // 消息创建时间
2.3. 效果验证
客户端显示问题
前端接收报文
返回时间属性:createTime
[{
"id": "62c11d3435b7c4007a8e650e",
"fromUserId": "21100598TZ9XG6RP",
"fromNickname": "小美女",
"fromFace": "http://gblfy.cn:9000/imooc/xh.jpg",
"toUserId": "220620BZ2DH0KP94",
"msgType": 1,
"msgContent": {
"isFriend": true
},
"createTime": "2022-07-03 12:38:11"
},
...其他数据忽略
]
- 正常时间为:
2022-07-03 12:38:11
测试结果:返回数据时间与正确落库一致
边栏推荐
- Nuxt reports an error: render function or template not defined in component: anonymous
- View CSDN personal resource download details
- Implementing expired localstorage cache with lazy deletion and scheduled deletion
- Basic data types in golang
- Fatal error in golang: concurrent map writes
- Regular expression (I)
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- How web pages interact with applets
- Kotlin:集合使用
- 入职中国平安三周年的一些总结
猜你喜欢
Hands on deep learning (34) -- sequence model
JDBC and MySQL database
C语言指针经典面试题——第一弹
How web pages interact with applets
【OpenCV 例程200篇】218. 多行倾斜文字水印
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
Log cannot be recorded after log4net is deployed to the server
C # use gdi+ to add text with center rotation (arbitrary angle)
Histogram equalization
Logstack configuration details -- elasticstack (elk) work notes 020
随机推荐
Go context basic introduction
Golang defer
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Go context 基本介绍
Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
Get the source code in the mask with the help of shims
Lauchpad x | MODE
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Log cannot be recorded after log4net is deployed to the server
Golang Modules
AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
Golang defer
Hands on deep learning (32) -- fully connected convolutional neural network FCN
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
Are there any principal guaranteed financial products in 2022?
View CSDN personal resource download details
Dynamic memory management
About the for range traversal operation in channel in golang
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
el-table单选并隐藏全选框