Java mongodb ISODate 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20941687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodb ISODate problems
提问by wuchang
I am using java(IDE is eclipse) to query on mongodb. Below is my java code:
我正在使用 java(IDE 是 eclipse)在 mongodb 上进行查询。下面是我的java代码:
DBObject query = new BasicDBObject();
ObjectId id =new ObjectId("529f280b90ee58cb7732c2b8");
query.put("_id", id);
DBCursor cursor = collection.find(query);
while(cursor.hasNext()) {
DBObject object = (DBObject)(cursor.next());
System.out.println(object.get("_id"));
System.out.println(object.get("createDate"));
}
Problems happened in the createDate whose type is ISODate and value is ISODate("2013-10-21T01:34:04.808Z")
, but the println
result of my code is 'Mon Oct 21 **09**:34:04 CST 2013'
, the hour has changed from 01 to 09. I don't know what happened!
类型为 ISODate 且值为 的 createDate 出现问题ISODate("2013-10-21T01:34:04.808Z")
,但println
我的代码的结果是'Mon Oct 21 **09**:34:04 CST 2013'
,小时已从 01 更改为 09。我不知道发生了什么!
Can anybody help?
有人可以帮忙吗?
回答by Basil Bourque
The hour did not change. You must be in China, given the "CST" in your example and the 8 hour difference. If you interpret "CST" as "China Standard Time" (rather than Central Standard Timein the US), then you have a time zone that is 8 hours ahead of UTC/GMT. So when ti is 1 AM UTC/GMT, at the vary same moment the clock on the wall in Taipei will read "9 AM".
时间没有改变。鉴于您的示例中的“CST”和 8 小时的差异,您必须在中国。如果您将“CST”解释为“中国标准时间”(而不是美国的中部标准时间),那么您的时区比UTC/GMT早 8 小时。因此,当 ti 是 UTC/GMT 凌晨 1 点时,同一时刻台北墙上的时钟会显示“9 AM”。
Minor point: Those three-letter codes for time zones are obsolete and should be avoided. They are neither standardized nor unique. Use proper time zone names.
小点:时区的那些三字母代码已过时,应避免使用。它们既不标准化也不独特。使用正确的时区名称。
Major point: The problem lies in how you extract a value from MongoDB that represents a date-time.
要点:问题在于如何从 MongoDB 中提取表示日期时间的值。
I don't know MongoDB, and their docis confusing, so I can't help you much further. If you can retrieve an ISO 8601string as seen in your first example, that is much preferable to the format of your second example.
我不知道 MongoDB,而且他们的文档令人困惑,所以我无法进一步帮助您。如果您可以检索第一个示例中看到的ISO 8601字符串,那么这比第二个示例的格式要好得多。
If you want to work with the date-time value in Java, you can feed an ISO 8601 string directly to a DateTime
constructor in Joda-Time2.3.
如果要使用 Java 中的日期时间值,可以将 ISO 8601 字符串直接提供给Joda-Time2.3 中的DateTime
构造函数。
DateTime dateTime = new DateTime( "2013-10-21T01:34:04.808Z" );
Update
更新
This docsays that the Java driver for MongoDB will give you a java.util.Date object. That explains your problem. The java.util.Date & Calendar classes bundled with Java are notoriously bad. One problem is that while a Date instance has no time zone, its toString()
method uses the JVM's default time zone to render a string. And Date's toString method uses that terrible ambiguous format.
这个文档说 MongoDB 的 Java 驱动程序会给你一个 java.util.Date 对象。这解释了你的问题。与 Java 捆绑在一起的 java.util.Date 和 Calendar 类是出了名的糟糕。一个问题是,虽然 Date 实例没有时区,但它的toString()
方法使用 JVM 的默认时区来呈现字符串。而 Date 的 toString 方法使用了那种可怕的模棱两可的格式。
You should avoid using java.util.Date & Calendar classes. For now use the Joda-Timelibrary. In Java 8, you can use the new java.time.* classes.
您应该避免使用 java.util.Date 和 Calendar 类。现在使用Joda-Time库。在 Java 8 中,您可以使用新的java.time.* classes。
You can convert back and forth between java.util.Date and Joda-Time. Pass a Date instance to Joda-Time constructor. To go back, call Joda-Time toDate()
format.
您可以在 java.util.Date 和 Joda-Time 之间来回转换。将 Date 实例传递给 Joda-Time 构造函数。要返回,请调用 Joda-TimetoDate()
格式。
Note that while a java.util.Date has no time zone information within it, in contrast a DateTime object does have a time zone assigned. If you want UTC/GMT, specify DateTimeZone.UTC
.
请注意,虽然 java.util.Date 中没有时区信息,但相比之下,DateTime 对象确实分配了时区。如果您需要 UTC/GMT,请指定DateTimeZone.UTC
.
Your code should look more like:
您的代码应该看起来更像:
java.util.Date date = object.get("createDate");
DateTime createDateTime = new DateTime( date, DateTimeZone.forId( "Asia/Manila" ) );
System.out.println( createDateTime );
… do some work …
java.util.Date dateGoingBackToMongoDB = createDateTime.toDate();