当前位置:网站首页>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
测试结果:返回数据时间与正确落库一致
边栏推荐
- 自动化的优点有哪些?
- View CSDN personal resource download details
- PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
- Kotlin set operation summary
- Sort out the power node, Mr. Wang he's SSM integration steps
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- 2022-2028 global protein confectionery industry research and trend analysis report
- `Example of mask ` tool use
- Dynamic address book
- 5g/4g wireless networking scheme for brand chain stores
猜你喜欢
Dynamic memory management
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
Devop basic command
Advanced technology management - how to design and follow up the performance of students at different levels
Hands on deep learning (40) -- short and long term memory network (LSTM)
Hands on deep learning (46) -- attention mechanism
Custom type: structure, enumeration, union
华为联机对战如何提升玩家匹配成功几率
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
随机推荐
2022-2028 global seeder industry research and trend analysis report
Exercise 8-7 string sorting (20 points)
Latex download installation record
Hands on deep learning (44) -- seq2seq principle and Implementation
Modules golang
Hands on deep learning (43) -- machine translation and its data construction
Dynamic memory management
Lauchpad x | MODE
Explanation of closures in golang
Intelligent gateway helps improve industrial data acquisition and utilization
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
转载:等比数列的求和公式,及其推导过程
About the for range traversal operation in channel in golang
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
用数据告诉你高考最难的省份是哪里!
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Are there any principal guaranteed financial products in 2022?
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
Hands on deep learning (46) -- attention mechanism
Pueue data migration from '0.4.0' to '0.5.0' versions