Using Date to store dateTime

Posted by sandyrandhawa:

Hi,

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?
Thanks.

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()
            .enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
            .enable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES)
            .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
            .enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)
            .enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .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.