MongoDB,Java:将日期属性检索为UTC [英] MongoDB, Java: Retrieve date property as UTC

查看:62
本文介绍了MongoDB,Java:将日期属性检索为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些实体保存到Mongo数据库中,这些实体具有Joda DateTime属性,这些属性已将UTC设置为时区.虽然保存效果很好,并且我在集合中看到了具有正确值的属性,但是当我通过Java检索实体后,时区将再次设置为UTC + 2.

I'm saving some entities to a Mongo database, these have Joda DateTime properties which have UTC set as a timezone. While saving works fine and I see the properties with correct values in the collection, once I retrieve the entities through Java the timezone gets set to UTC+2 again.

这是在集合中:

"created" : ISODate("2013-07-26T20:36:57.890Z")

我正在使用Spring-Data-MongoDB访问数据库.

I'm using Spring-Data-MongoDB to access the database.

Category category = mongoTemplate.findById(id, Category.class);

最后我得到了这个

2013-07-26T23:05:56.439+02:00

有没有办法告诉Mongo返回存储在日期中的时区?

Is there a way to tell Mongo do return the timezone stored in the date?

提示表示感谢,谢谢!

推荐答案

驱动程序以java.util.Date对象的形式返回数据库所具有的内容.它对时间所代表的时区一无所知.它不会在任何地方存储时区. Mongo Shell始终将时间值显示为UTC.

The driver is returning what the database has as a java.util.Date object. It knows nothing of the timezone that time represents. It does not store the Timezone anywhere. Mongo Shell always presents a time value as UTC.

话虽这么说,如果您想始终像UTC一样在应用程序代码中使用它,我认为有一种方法可以告诉JODA库做到这一点:

That being said, if you want to work with it in your application code as UTC always, I think there is a way to tell the JODA library to do this: Defaulting date time zone to UTC for Jodatime's DateTime

这篇关于MongoDB,Java:将日期属性检索为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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