当前位置:网站首页>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
测试结果:返回数据时间与正确落库一致
边栏推荐
- Summary of the most comprehensive CTF web question ideas (updating)
- PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
- Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
- Vanishing numbers
- Hands on deep learning (34) -- sequence model
- C # use smtpclient The sendasync method fails to send mail, and always returns canceled
- ASP. Net to access directory files outside the project website
- Deadlock in channel
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- 浅谈Multus CNI
猜你喜欢

Hands on deep learning (35) -- text preprocessing (NLP)

今日睡眠质量记录78分

How web pages interact with applets

C language pointer interview question - the second bullet

Hands on deep learning (39) -- gating cycle unit Gru

Hands on deep learning (36) -- language model and data set

Fabric of kubernetes CNI plug-in

PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql

5g/4g wireless networking scheme for brand chain stores

2. Data type
随机推荐
Histogram equalization
转载:等比数列的求和公式,及其推导过程
Kubernetes CNI 插件之Fabric
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
技术管理进阶——如何设计并跟进不同层级同学的绩效
Hands on deep learning (35) -- text preprocessing (NLP)
Dynamic memory management
How does idea withdraw code from remote push
Get the source code in the mask with the help of shims
Kotlin:集合使用
用数据告诉你高考最难的省份是哪里!
Qtreeview+ custom model implementation example
Exercise 9-5 address book sorting (20 points)
Write a jison parser from scratch (6/10): parse, not define syntax
libmysqlclient.so.20: cannot open shared object file: No such file or directory
Golang type comparison
Exercise 7-2 finding the maximum value and its subscript (20 points)
Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
C语言指针经典面试题——第一弹
C # use smtpclient The sendasync method fails to send mail, and always returns canceled