Search code examples
javamongodbjava-8morphiazoneddatetime

ZonedDateTime in MongoDB through Morphia


In a nutshell:

  • java.time.ZonedDateTime has no args constructor (actually it has no
    constructor at all)
  • to get over this, I should write a ZonedDateTimeConverter for Morphia
  • if I try to save and load my ZonedDateTime without converter, Morphia saves the full ZonedDateTime object as DBObject which is cool because I could read all the fields and initiate a ZonedDateTime object from it, but I got an exception on loading: No usable constructor for java.time.ZonedDateTime
  • if I try to save and load the ZonedDateTime with the converter, I got another exception: Can't find a codec for class java.time.LocalDateTime. I started to play with the converters, based on this article but without any result.
  • So if I have no converter I can save but can't load, if I have the converter, I can't even save because of missing codecs.

Any idea?

UPDATE: Turned out that I should write a converter for each subtype started from ZonedDateTime (e.g. LocalDateTime, LocalDate, LocalTime), but turned out that I should write a converter for java.time.ZoneRegion which is package-private.


Solution

  • I pushed support for the more common Java 8 datetime types to Morphia last week targeting the 1.3 release. See the issue here and the linked commits. If there's a type you need that's not listed, please file a Morphia issue and I'll try to make sure it's included in 1.3 as well.