在mongodb中保存带日期的TimeZone [英] Save TimeZone with Date in mongodb

查看:373
本文介绍了在mongodb中保存带日期的TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档类中有java.util.Date字段.

I have java.util.Date field in my document class.

E:g:

@Document(collection = "testdoc")
public class TestDoc {
    @Id
    String id;
    Date startDate;
}

即使我使用UTC和IST设置了日期,也始终将其保存在我的收藏夹中,如下所示,

Even if I set the Date with UTC and IST, it always save in my collection as below,

 "startDate" : ISODate("2015-08-21T18:30:00.000Z")

如何在mongo集合中保存时区? Z在这种情况下代表什么?

How can I save the time zone also in mongo collection? What does Z stand in this case?

推荐答案

"Z"表示时间存储在UTC中.在存储之前,Mongo会在UTC内部转换所有时间的本地表示形式.但是,一种建议是将时间与从您的应用程序接收到的时区一起存储.稍后,您可以在应用程序逻辑中根据UTC时间和时区重构本地时间.

The 'Z' signifies that the time is stored in UTC. Mongo internally converts all local representations of time in UTC before storing. However, one suggestion would be to store the time along with the timezone which is received from your application. You can later reconstruct the local time from the UTC time and the timezone in your application logic.

请通过此链接.他们提供了有关如何使用JavaScript建模本地时间数据的示例.

Please go through this link. They have given an example on how to model local time data using JavaScript.

https://docs.mongodb.com/v3.2/tutorial/model-time-data/

这篇关于在mongodb中保存带日期的TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆