0

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.

Community
  • 1
  • 1
maestro
  • 671
  • 1
  • 13
  • 27

1 Answers1

0

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.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • I imagine this is a popular use case, but if we want to lock down to a particular instant in time and be immune to daylight savings or different server timezones, it looks like the java8 way of doing things is this ZonedDateTime.now(ZoneOffset.UTC)? How should we be using this with morphia so that it is storable and serial/deserializable? – Justin Nov 25 '16 at 16:37