Skip to content

Commit ff12786

Browse files
committed
In JsonReader, replaced SimpleDateFormat with DatatypeConverter.parseDateTime, as Java 6 does not support the 'X' pattern character.
JAVA-1768
1 parent abe65f7 commit ff12786

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

bson/src/main/org/bson/json/JsonReader.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -875,18 +875,12 @@ private long visitDateTimeExtendedJson() {
875875
if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64) {
876876
return valueToken.getValue(Long.class);
877877
} else if (valueToken.getType() == JsonTokenType.STRING) {
878-
String dateString = valueToken.getValue(String.class);
879-
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
880-
ParsePosition pos = new ParsePosition(0);
881-
format.setLenient(true);
882-
883-
Date date = format.parse(dateString, pos);
884-
885-
if (date != null && pos.getIndex() == dateString.length()) {
886-
return date.getTime();
878+
String dateTimeString = valueToken.getValue(String.class);
879+
try {
880+
return DatatypeConverter.parseDateTime(dateTimeString).getTimeInMillis();
881+
} catch (IllegalArgumentException e) {
882+
throw new JsonParseException("JSON reader expected an ISO-8601 date time string but found.", dateTimeString);
887883
}
888-
889-
throw new JsonParseException("JSON reader expected an ISO-8601 date string but found.", dateString);
890884
} else {
891885
throw new JsonParseException("JSON reader expected an integer or string but found '%s'.", valueToken.getValue());
892886
}

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testDateTimeISOStringWithTimeOffset() {
147147

148148
@Test(expected = JsonParseException.class)
149149
public void testInvalidDateTimeISOString1() {
150-
String json = "{ \"$date\" : \"2015-04-16T16:55:57.626+02:000\" }";
150+
String json = "{ \"$date\" : \"2015-04-16T16:55:57.626+02:0000\" }";
151151
bsonReader = new JsonReader(json);
152152
bsonReader.readBsonType();
153153
}

0 commit comments

Comments
 (0)