|
1.java code:
String bsonStr = "{\"time\" : { \"$date\" : \"2012-06-18T02:37:43.942+0800\" }}";
|
BasicDBObject bo = (BasicDBObject) JSON.parse(bsonStr);
|
System.out.println(bo.toMap().get("time")); // result: null
|
Why not 2012-06-18T02:37:43.942 or Wed Jun 18 02:37:43 CST 2014?
zone: Asia/Shanghai
bsonStr = "{\"time\" : { \"$date\" : \"2012-06-18T02:37:43.942Z\" }}";
|
bo = (BasicDBObject) JSON.parse(bsonStr);
|
System.out.println(bo.toMap().get("time")); // result: Wed Jun 18 10:37:43 CST 2012
|
2.mongo java driver source code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
|
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
|
if (o == null) {
|
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
|
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
|
}
|
the need to add format "yyyy-MM-dd'T'HH:mm:ss.SSSZ" and "yyyy-MM-dd'T'HH:mm:ssZ" or Joda Time or JDK 8 datatime class ?
example:
String _msDateFormats = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
SimpleDateFormat format = new SimpleDateFormat(_msDateFormats);
|
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
|
Object o = format.parse(/*b.get("$date")*/"2014-06-18T02:37:43.942+0800".toString(), new ParsePosition(0));
|
System.out.println(o); // Wed Jun 18 02:37:43 CST 2014
|
Reporter: oo
E-mail: 515951184@163.com
|