Spring Mongodb Timestamp Timezone Misleading

依然范特西╮ 提交于 2019-12-10 22:56:19

问题


I am using Spring Data MongoDB. When I save some records MongoDb doesn't save correctly my timestamp.

Here is my timestamp field in Spring.

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date timestamp = new Date();

My MongoDB record.

{
"_id": ObjectId("5697a672ce2a8e5347d86afd"),
"batteryLevel": 100,
"beaconClass": 3,
"beaconId": "dsadsa",
"timestamp": ISODate("2016-01-14T13:45:22.702Z")
}

When I log to console my timezone and date I see it is correct.

Eastern European Time
Asia/Istanbul
Thu Jan 14 15:45:22 EET 2016

How can I correct time MongoDB timestamp?


回答1:


MongoDB stores times in UTC by default, and will convert any local time representations into this form, see the documentation. You will have to compute the original local time in your application logic.




回答2:


I am also using spring data with spring boot. Even if Mongo is storing my current timezone (CEST) in UTC, when I retrieve it with queries from a MongoRepository, it does the conversion automatically and retrieves the desired data.



来源:https://stackoverflow.com/questions/34791481/spring-mongodb-timestamp-timezone-misleading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!