Introduction
I am currently working on a project that regularly saves several java objects to a MongoDB database.
This object contains a Date.
Problem
The conversion of java Date to Json Mongo give this:
new Date() -> "Jan 27, 2022, 2:47:24 AM"
But this format does not conform with MongoDB Date. MongoDB is considered as a string instead of a Date.
Affected code
public record DatedPuzzle(Date date, Puzzle puzzle) {
}
List<InsertOneModel<Document>> bulkWrites = puzzles.parallelStream()
.map(puzzle -> new InsertOneModel<>(Document.parse(gson.toJson(puzzle))))
.toList();
Question
How to create an object conforming to Gson's serialization?
For example:
new Date() -convert with Gson-> "2022-01-27T01:47:24.000+00:00"