Using Date to store dateTime

Posted by sandyrandhawa:


We have a schema in DGraph where one of the predicate is of dateTime type and we are trying to use java.util.Date type to represent it in our data model POJO.

So the issue is that when we convert it to json string and try to store it using setSetJson no exception is thrown out and also all other fields are being stored other than dateTIme field.

When we change the POJO field from java.util.Date to String and format date it using SimpleDateFormat and then everything is running as expected.

So is there an issue with using java.util.Date or is it just not supported right now?

fendor commented :

Did you take a look at the produced json? when using Jackson, probably Gson as well, a Date object is serialized into some kind of list.
The solution for Jackson is to add modules that serialize Date objects correctly. For example, I use the following ObjectMapper:

    private static final ObjectMapper objMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule());

Moreover, I encountered multiple problems wen using Date or DateTime, regarding some missing characters, such as a missing ā€œZā€ at the end of the serialized String.
Therefore, I am using Instant at the moment, which did not cause these problems so far.