gpt4 book ai didi

java - 我在 MongoDB 中存储了一个带有 UNIX 时间戳的 JAVA 日期并得到了错误的答案

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:49:14 25 4
gpt4 key购买 nike

当我将 UNIX 时间戳存储到 MongoDB 中时,我遇到了一个让我的数据库变得糟糕的大问题。这是代码:

DBObject tmp = new BasicDBObject("_id", basicUrl+ob.getString("id"));//new a DBObject,it doesn't matter
Date date= new Date(1316410912);//1316410912 is a UNIX timestamp
tmp.put("created_at",date);
mg = new Mongo();
test=db.getCollection("qq");
test.insert(tmp);

完成添加过程,打印查询结果:

{ "_id" : "http://t.qq.com/p/t/163173122485971" , "created_at" : { "$date" : "1970-01-16T19:51:58.125Z"}}

但实际时间应该是“Mon Sep 19 2011 13:41:52”

请帮助我并告诉我我的代码有什么问题。

最佳答案

Date 以毫秒为单位,而不是以秒为单位。

 Date date= new Date(1316410912000l);

这将产生正确的日期。参见 Javadocs了解更多详情。

关于java - 我在 MongoDB 中存储了一个带有 UNIX 时间戳的 JAVA 日期并得到了错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com