Jen-Ming Chung

Converting ISODate from MongoDB

I’m confused about the insertion operation with Date object via mongo-java-driver that always short of 8 hours where my place of residence is Taiwan (GMT+8). According to the enclosed references, we can observe that the incoming Date object will be set to ISO_8601_DATE_FORMAT as shown in the following code snippet.

if (o instanceof Date) {
    Date d = (Date) o;
    SimpleDateFormat format = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
    serialize(new BasicDBObject("$date", format.format(d)), buf);

An example next demonstrates that the given Date will plus 8 hours in the GMT+8 time zone [^3].

SimpleDateFormat format = 
    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
Date date = format.parse("2012-01-20T00:00:00.000Z");