使用Joda-Time为Mongo插入形成正确的ISODate [英] Using Joda-Time to form correct ISODate for Mongo insert

查看:463
本文介绍了使用Joda-Time为Mongo插入形成正确的ISODate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新需要ISODate格式的mongo中的日期字段。在mongo中,它看起来像这样:

I am trying to update date fields in mongo that require an ISODate format. In mongo, it looks like this:

"crDt" : ISODate("2013-08-19T17:21:57.549Z")

我使用的Java框架限制使用字符串作为我的测试参数,所以我是尝试将该字符串与 DateTimeFormatter 一起使用,以使其进入正确的 ISODateTimeFormat ,然后将其传递给mongo。我不能只传入一个看起来像我上面的字符串。试图这样做搞砸了mongo的领域。我正在使用的 Joda-Time 代码的相关内容如下所示:

The Java framework I am using has me restricted to using strings as my test parameters, so I am trying to use that string with a DateTimeFormatter to get it into the correct ISODateTimeFormat and then pass that into mongo. I cannot just pass in a string that looks like what I have above. Trying to do so screws up the field in mongo. The relevant bits of Joda-Time code I am using look like this:

//I can't get this right.
String crDt = "2013-01-19T15:28:58.851Z";

DateTimeFormatter parser = ISODateTimeFormat.dateHourMinuteSecondMillis();

parser.parseDateTime(crDt);

// this method updates the record in mongo. This method totally works, so no 
// point in pasting it here, I just can't get the parser object correct to be 
// in the correct format once inserted, it needs to be the correct ISODate form.
mongo.setCrDt(recordId, parser);

当代码运行时,我从.parseDateTime方法中得到这样的错误:

And when the code runs I get errors like these from the .parseDateTime method:

java.lang.IllegalArgumentException: Invalid format: "2013-01-19T15:28:58.851Z" is malformed at "Z"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)

我可以告诉我给出的字符串是不正确的解析。我试过离开 Z ,我尝试了其他组合,但每次都说它的格式不正确。所以基本上,我的起始字符串需要是什么才能让 .parseDateTime 工作并给我一个看起来正确的对象?

I can tell the string I am giving is not correct to get things parsed. I've tried leaving off the Z, I've tried other combos, but each time it says it's malformed. So basically, what does my starting string need to be to get the .parseDateTime to work and give me an object that looks correct?

编辑:

更新以尝试下面提供的建议。我现在遇到的问题是IllegalArgumentException,无法序列化类org.joda.time.DateTime。所以它似乎坚持joda时间对象在禁止?我还查看了另一个建议,查看了像Spring Data这样的mapper框架。看起来还有很多东西需要进入这个。真的没有简单的方法可以将其持久化为mongo吗?

Updated to try the suggestions provided below. The issue I run into now is an IllegalArgumentException, can't serialize class org.joda.time.DateTime. So it appears persisting joda time objects in a no-go? I also looked at the other suggestion, looking into mapper frameworks like Spring Data. It looks like there is a whole lot more that needs to go into this. Is there really no simple way to persist this into mongo?

EDIT2:

好的,我想我现在拥有它。我可能没有完全掌握所有的机制,但 BasicDBObject s与 DateTime 。日期对象似乎是唯一的方法,至少在我正在处理的实现中。我做了以下事情:

OK, I think I have it now. I might not have a total grasp of all the mechanics at play, but BasicDBObjects won't play nice with DateTime. Date objects seem to be the only way to go, at least in the implementation I'm dealing with. I did the following:

DateTimeFormatter parser = ISODateTimeFormat.dateTime();
DateTime result;
Date newResult;
result = parser.parseDateTime(crDt);
newResult = result.toDate();

然后我在newResult中传递BasicDBObject,然后在mongo中更新记录。它工作正常,记录正确更新。

I then passed in newResult for the BasicDBObject to then update the record in mongo. It works fine, and the record is updated correctly.

推荐答案

您的输入字符串格式是正确的,只要它用于表示UTC。

Your input string format is correct, as long is that is intended to represent UTC.

更改解析器以使用与此格式匹配的解析器:

Change your parser to use the one that matches this format:

DateTimeFormatter parser = ISODateTimeFormat.dateTime();

你的其余问题对我来说没什么意义。你不应该传递解析器,而是来自 parseDateTime 的返回值,你似乎不是捕获。

The rest of your question doesn't make much sense to me. You shouldn't pass the parser, but rather the return value from parseDateTime, which you don't appear to be capturing.

DateTime result = parser.parseDateTime(crDt);

mongo.setCrDt(recordId, result.toDate());

最后一行是否有效取决于该函数接受的内容。

Whether or not that last line will work depends on what that function accepts.

这篇关于使用Joda-Time为Mongo插入形成正确的ISODate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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