Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-395

Date Time values do not preserve milliseconds when serialized to or deserialized from JSON

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.6.4
    • Affects Version/s: 2.6.2, 2.6.3
    • Component/s: None

      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.

            Assignee:
            antoine Antoine Girbal
            Reporter:
            jgro jgro
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: