Lets say I have a class User. A user got a name, gender, …, born. When I got an userobject I can insert that in the database. Thats fine. There is just a problem with date. Im trying this:
String json = "{'name': '" + user.getName() + "', 'born': '" + user.getBorn() + "'}";
DBObject o = (DBObject)JSON.parse(json);
myCollection.insert(o);
user.getBorn() is returning a Date, but in the database it is a string. So if Im reading it I get something like Fri Sep 05 00:00:00 GMT+01:00 1980 as a string and thats not good 🙂 I would like have a Date. So there is maybe more the one option(?) Maybe I could convert this DateString somehow to Date? Or maybe I could insert the document without a string? Thank you for any help!
Edit: ok Renato said I could use simpledateformat. And whats about the other option? How to put it as a Date() in the db?
It might be easier to use the
BasicDBObjectBuilderlike so:This will store the born value as a BSON date.