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 1st code snippet. The 2nd example demonstrates that the given Date will plus 8 hours in the GMT+8 time zone [2].

if (o instanceof Date) {
    Date d = (Date) o;
    SimpleDateFormat format = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
    serialize(new BasicDBObject("$date", format.format(d)), buf);
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");


  1. serializing and deserializing of java.util.Date can now handle timezone …
  2. mongo java driver日期转换问题
  3. A guide to Java SimpleDateFormat in examples

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s