I'm using Gson to serialize my javabean in a JSON, and I'm having problems with Date type fields.
If I create GsonBuilder without any redefinition
Gson gson = new GsonBuilder().create();
it generates the Date field in JSON in the format "Jan 6, 2017 12:00:00 AM", but with the data type String.
If I set the builder to a date format
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
It generates the field in the desired format, but remains as String.
With the String type, I can't do query based on periods (like this). Manually changing one of the records, I was able to get response:
before:
{"_id" : ObjectId("586fa17851ba381278b059ac"),
"start" : "2017-01-06"),
...
}
after:
{"_id" : ObjectId("586fa17851ba381278b059ac"),
"start" : ISODate("2017-01-06T12:00:00.000Z"),
...
}
How can I ensure the generation of a Date field using Gson?