Skip to content

Commit 4e96ee3

Browse files
committed
- JAVA-395: Date Time values do not preserve milliseconds when serialized to or deserialized from JSON
1 parent b4d4244 commit 4e96ee3

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/main/com/mongodb/MongoOptions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ else if (safe)
160160
*/
161161
public boolean fsync;
162162

163+
/**
164+
* The "j" value of the global WriteConcern.
165+
* Default is false.
166+
*/
167+
public boolean j;
168+
163169
public String toString(){
164170
StringBuilder buf = new StringBuilder();
165171
buf.append( "description=" ).append( description ).append( ", " );
@@ -174,7 +180,8 @@ public String toString(){
174180
buf.append( "safe=" ).append( safe ).append( ", " );
175181
buf.append( "w=" ).append( w ).append( ", " );
176182
buf.append( "wtimeout=" ).append( wtimeout ).append( ", " );
177-
buf.append( "fsync=" ).append( fsync );
183+
buf.append( "fsync=" ).append( fsync ).append( ", " );
184+
buf.append( "j=" ).append( j );
178185

179186
return buf.toString();
180187
}

src/main/com/mongodb/util/JSON.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static void serialize( Object o , StringBuilder buf ){
139139
if (o instanceof Date) {
140140
Date d = (Date) o;
141141
SimpleDateFormat format =
142-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
142+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
143143
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
144144
serialize(new BasicDBObject("$date", format.format(d)), buf);
145145
return;

src/main/com/mongodb/util/JSONCallback.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
package com.mongodb.util;
44

5+
import java.text.ParseException;
56
import java.text.ParsePosition;
67
import java.text.SimpleDateFormat;
78
import java.util.*;
9+
import java.util.logging.Level;
10+
import java.util.logging.Logger;
811
import java.util.regex.Pattern;
912

1013
import org.bson.*;
@@ -45,9 +48,17 @@ public Object objectDone(){
4548
}
4649
} else if ( b.containsField( "$date" ) ) {
4750
SimpleDateFormat format =
48-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
49-
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
50-
o = format.parse((String)b.get("$date"), new ParsePosition(0));
51+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
52+
GregorianCalendar calendar = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
53+
format.setCalendar(calendar);
54+
String txtdate = (String) b.get("$date");
55+
o = format.parse(txtdate, new ParsePosition(0));
56+
if (o == null) {
57+
// try older format with no ms
58+
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
59+
format.setCalendar(calendar);
60+
o = format.parse(txtdate, new ParsePosition(0));
61+
}
5162
if (!isStackEmpty()) {
5263
cur().put( name, o );
5364
} else {

src/test/com/mongodb/util/JSONTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void testObjectId() {
288288
@org.testng.annotations.Test
289289
public void testDate() {
290290
Date d = new Date();
291-
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
291+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
292292
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
293293
String formattedDate = format.format(d);
294294

@@ -297,6 +297,7 @@ public void testDate() {
297297

298298
Date d2 = (Date)JSON.parse(serialized);
299299
assertEquals(d.toString(), d2.toString());
300+
assertTrue(d.equals(d2));
300301
}
301302

302303
@org.testng.annotations.Test

0 commit comments

Comments
 (0)