[JAVA-395] Date Time values do not preserve milliseconds when serialized to or deserialized from JSON Created: 15/Jul/11  Updated: 10/Aug/11  Resolved: 19/Jul/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.6.2, 2.6.3
Fix Version/s: 2.6.4

Type: Bug Priority: Minor - P4
Reporter: jgro Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: bson
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Mongo and BSON Date types store time with millisecond precision. Java's java.util.Date class does too. However, com.mongodb.util.JSON.serialize() outputs Date strings without milliseconds using SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") and com.mongodb.util.JSON.parse() does not accept date strings containing milliseconds.

This leads to

Date date = new Date();
Date truncated = (Date) JSON.parse(JSON.serialize(date));
System.out.println("Dates are equal? " + date.equals(truncated));

to print "Dates are equal? false"

Date format should instead be SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").

Currently, JSON.parse() does not accept time strings that include milliseconds, returning null instead of a Date object. It should accept strings with milliseconds, and maintain backwards compatibility (for people who have stored JSON strings somewhere) by continuing to accept strings without milliseconds.



 Comments   
Comment by auto [ 19/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: - JAVA-395: Date Time values do not preserve milliseconds when serialized to or deserialized from JSON
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/4e96ee32492ce7a97afc9aebfd32c43ff29016d8

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