title: Using Joda-Time to form correct ISODate for Mongo insert
body: <p>I am trying to update date fields in mongo that require an ISODate format. In mongo, it looks like this:</p>
  
  <pre><code>"crDt" : ISODate("2013-08-19T17:21:57.549Z")
  </code></pre>
  
  <p>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 <code>DateTimeFormatter</code> to get it into the correct <code>ISODateTimeFormat</code> 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 <a href="http://www.joda.org/joda-time/" rel="noreferrer">Joda-Time</a> code I am using look like this:</p>
  
  <pre><code>//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);
  </code></pre>
  
  <p>And when the code runs I get errors like these from the .parseDateTime method:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I can tell the string I am giving is not correct to get things parsed. I've tried leaving off the <code>Z</code>, 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 <code>.parseDateTime</code> to work and give me an object that looks correct?</p>
  
  <p>EDIT:</p>
  
  <p>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? </p>
  
  <p>EDIT2:</p>
  
  <p>OK, I think I have it now. I might not have a total grasp of all the mechanics at play, but <a href="http://api.mongodb.org/java/current/com/mongodb/BasicDBObject.html" rel="noreferrer">BasicDBObject</a>s won't play nice with <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html" rel="noreferrer">DateTime</a>. Date objects seem to be the only way to go, at least in the implementation I'm dealing with. I did the following:</p>
  
  <pre><code>DateTimeFormatter parser = ISODateTimeFormat.dateTime();
  DateTime result;
  Date newResult;
  result = parser.parseDateTime(crDt);
  newResult = result.toDate();
  </code></pre>
  
  <p>I then passed in newResult for the BasicDBObject to then update the record in mongo. It works fine, and the record is updated correctly. </p>