MongoDB java-driver insert date

krampstudio picture krampstudio · Jan 29, 2013 · Viewed 8.7k times · Source

I'm using MongoDB 2.2 with java-driver 2.10.1

I'm inserting a date field into a document from a java.util.Date instance. My instance has the following value:

Wed Oct 10 00:00:00 CEST 2012

but once in mongo, I have this value: ISODate("2012-10-09T22:00:00Z")

My insertion code:

BasicDBObject doc =  new BasicDBObject("key", event.getKey())
                    .append("title", event.getTitle())
                    .append("description",  event.getDescription())
                    .append("date",  event.getDate());
db.getCollection("events").insert(doc);

You can have a look to the date instance referenced from my event object on this debug screenshot: enter image description here

Is there something to do with the timezone ? Or a bug from the driver ?

Answer

JohnnyHK picture JohnnyHK · Jan 29, 2013

Dates in MongoDB are always stored as UTC datetimes, so what you're seeing is correct.

The CEST time zone is two hours ahead of UTC (GMT) so your time's correct UTC representation is two hours earlier than your CEST time, which is exactly what you're seeing.