[JAVA-1354] JSON date parsing fails with timezones other than Z Created: 07/Aug/14  Updated: 14/Mar/17  Resolved: 14/Mar/17

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

Type: Bug Priority: Minor - P4
Reporter: Frej Bjon Assignee: Jeffrey Yemin
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Issue Links:
Duplicate
is duplicated by JAVA-1364 Comment on: "mongodb-ecosystem/driver... Closed
Related
related to DOCS-3879 Change in mongoexport output format f... Closed
Backwards Compatibility: Fully Compatible

 Description   

When the parser encounters a field with e.g.

{ "$date" : "2013-12-03T10:41:22.393+0200" }

the resulting value is null.

The reason is that the format string given to SimpleDateFormat is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" which requires a literal 'Z' rather than actually parsing the timezone.

Changing the string to "yyyy-MM-dd'T'HH:mm:ss.SSSX" should work better.



 Comments   
Comment by Jeffrey Yemin [ 09/Mar/17 ]

adamwasila, I investigated a bit more and found that as of MongoDB 3.0 mongoexport outputs dates in Zulu time:

~$ /mongodb/mongodb-3.0.default/bin/mongoexport -d test -c d
2017-03-09T18:45:30.226-0500	connected to: localhost
{"_id":{"$oid":"58c1e8ef28489b68ac9feeb8"},"d":{"$date":"2017-03-09T23:44:47.898Z"}}

Furthermore, MongoDB 2.6 will be EOLed this fall. Given that, I'm not convinced that this issue should be addressed.

Comment by Adam Wasila [ 21/Jan/17 ]

Ok, nailed it - I pushed pull request for this one. It has fallback code to work on 1.6 too and also some tests to cover this code branch when compiled in regular java8.

I made some performance tests and fallback code (so only if java 1.6 is used) seems to be 3x slower than regular one (1.7+). This is because of exception catch and all that code that happens later. It isn't that big deal I think, especially that old code would return null anyway, thus making timezoned dates useless.

Comment by Jeffrey Yemin [ 21/Jan/17 ]

We compile on java 8 but it has to run on java 6 with no required dependencies.

Comment by Adam Wasila [ 21/Jan/17 ]

I made some that simple fix and added it to the pull requests list. There is one side effect though - it will work only on java 7+ (but is that driver code compiling on 1.6 anyway?)

Comment by Jeffrey Yemin [ 07/Aug/14 ]

Note that this is a change in MongoDB 2.6:

$ /mongodb-2.2.default/bin/mongoexport -d test -c test
{ "_id" : { "$date" : 1407433938490 } }
 
$ /mongodb-2.4.default/bin/mongoexport -d test -c test
{ "_id" : { "$date" : 1407433938490 } }
 
$ /mongodb-2.6.default/bin/mongoexport -d test -c test
{ "_id" : { "$date" : "2014-08-07T13:52:18.490-0400" } }

Comment by Jeffrey Yemin [ 07/Aug/14 ]

Thanks for the bug report. We will schedule this into an upcoming release.

Comment by Frej Bjon [ 07/Aug/14 ]

I should also note that the JSON parser fails on mongoexport output, since mongoexport produces dates with the local timezone.

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