Can't save date format vertx mongodb

185 views
Skip to first unread message

Hai Nguyen

unread,
Sep 9, 2015, 8:25:31 AM9/9/15
to vert.x
I tried something like this 

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ssZ");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

String _fromTime = sdf.format(_fromDate);
String _toTime = sdf.format(_toDate);

ADD_JSON_OBJECT.put("publish_up", new JsonObject()
.put("$date", _fromTime));

ADD_JSON_OBJECT.put("publish_down", new JsonObject()
.put("$date", _toTime))
;

and get this error :( please help 

java.time.format.DateTimeParseException: Text '2015-01-08T06:00:00+0000' could not be parsed at index 19

at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)

at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)

at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)

Paulo Lopes

unread,
Sep 9, 2015, 9:11:43 AM9/9/15
to vert.x
From your stacktrace the issue is on your Date, you specify the parser to use timezone 'Z' Zulu and your String is using +0000 you need to fix your string to be: '2015-01-08T06:00:00Z'.

Hai Nguyen

unread,
Sep 9, 2015, 10:12:57 AM9/9/15
to vert.x
I removed the Z with no luck :( 

java.time.format.DateTimeParseException: Text '2015-01-08T13:00:00' could not be parsed at index 19


I think the problem is with 


ADD_JSON_OBJECT.put("publish_up",  new JsonObject()

                .put("$date", _fromTime));


since i comment it, no errors

Hai Nguyen

unread,
Sep 9, 2015, 10:33:28 AM9/9/15
to vert.x
Hmm 
It's seem the error occur when 

mongo.insert(DEAL_COLLECTION, ADD_JSON_OBJECT, res -> {


if (res.succeeded()) {

updateCounter();


} else {

res.cause().printStackTrace();

}


})
;

Paulo Lopes

unread,
Sep 9, 2015, 10:37:19 AM9/9/15
to vert.x
That does not make any sense because your stacktrace is on the class java.time.format.DateTimeParseException and the code you claim that you commented out is io.vertx.core.json.JsonObject which is not listed in your message at all.

The message states that your input date string is in an incorrect format.

Paulo Lopes

unread,
Sep 9, 2015, 10:52:25 AM9/9/15
to vert.x
Can you please share the json you are trying to insert and the full stacktrace, otherwise it will be very difficult to help you.

Hai Nguyen

unread,
Sep 9, 2015, 11:54:17 AM9/9/15
to vert.x
:D thanks but I resolve it. Get a way to generate ISO8601 date format :D  

public static String getISO8601StringForDate(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
Reply all
Reply to author
Forward
0 new messages