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 those posts 1 2, we can observe that the incoming Date object will be set to ISO_8601_DATE_FORMAT as shown in the following code snippet.

1
2
3
4
5
6
if (o instanceof Date) {
    Date d = (Date) o;
    SimpleDateFormat format = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
    serialize(new BasicDBObject("$date", format.format(d)), buf);
    return;
}

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

1
2
3
4
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");

Comments