[JAVA-1365] Comment on: "mongodb java driver JSON.parse($date) problem" Created: 05/Aug/14  Updated: 08/Dec/17  Resolved: 08/Dec/17

Status: Closed
Project: Java Driver
Component/s: Documentation
Affects Version/s: 2.12.3
Fix Version/s: None

Type: Improvement
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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



 Comments   
Comment by Jeffrey Yemin [ 08/Dec/17 ]

The newer JsonWriter class supports extended JSON format and no other work is planned for date parsing.

The com.mongodb.util.JSON class is deprecated, so we're not going to invest any more in that area of the code.

Generated at Thu Feb 08 08:54:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.