title: How to save a java.time.Instant in mongodb and load the same value out without exception?
body: <p>I would like a <code>java.time.Instant</code> to have the same value going in and out of mongo:</p>
  <pre><code>data class Person(@Id val name: String, val born: Instant)
  
  @ExtendWith(SpringExtension::class)
  @SpringBootTest
  class MongoMappingTest(@Autowired private val mongoTemplate: MongoTemplate)
  {
      @Test
      fun `Test custom mapping of the Instant data type`()
      {
          testFor(Instant.MAX)   // This throws java.lang.ArithmeticException
                                 // Failed to convert from type [java.time.Instant] to type [java.util.Date]
  
          testFor(Instant.now()) // This fails with:
                                 // Expected :Person(name=Jim, born=2021-04-07T16:56:10.838Z)
                                 // Actual   :Person(name=Jim, born=2021-04-07T16:56:10.838228297Z)
      }
  
      fun testFor(instant: Instant)
      {
          val jim = Person(&quot;Jim&quot;, instant)
          mongoTemplate.save(jim)
          val jimOut = mongoTemplate.findById(&quot;Jim&quot;, Person::class.java)
          Assertions.assertThat(jim).isEqualTo(jimOut)
      }
  }
  </code></pre>
  <p>The <code>Instant</code> first gets convereted to <code>java.util.Date</code> (no idea why this is happening), which in turn gets converted to the <code>BSON Date</code> data type. The latter has only milisecond precision, so we need to abandon it. I made my own representation and I am trying to substitute it (following the <a href="https://docs.spring.io/spring-data/data-mongodb/docs/current-SNAPSHOT/reference/html/#mapping-configuration" rel="nofollow noreferrer">docs</a>):</p>
  <pre><code>data class InstantRepresentation
  (
      val seconds: Long, val nanoseconds: Int
  )
  
  class InstantWriteConverter: Converter&lt;Instant, InstantRepresentation&gt;
  {
      override fun convert(source: Instant): InstantRepresentation
      {
          return InstantRepresentation(source.epochSecond, source.nano)
      }
  }
  
  class InstantReadConverter: Converter&lt;InstantRepresentation, Instant&gt;
  {
      override fun convert(source: InstantRepresentation): Instant?
      {
          return Instant.ofEpochSecond (source.seconds, source.nanoseconds.toLong())
      }
  }
  
  @Configuration
  class MongoConfig: AbstractMongoClientConfiguration()
  {
      public override fun getDatabaseName(): String
      {
          return &quot;db-test&quot;
      }
  
  
      public override fun configureConverters(adapter: MongoConverterConfigurationAdapter)
      {
          adapter.registerConverter(InstantWriteConverter())
          adapter.registerConverter(InstantReadConverter())
      }
  }
  </code></pre>
  <p>However, nothing has changed. My conversion code is not picked up. It seems that spring is still trying to convert my <code>Instant</code> into a <code>java.util.Date</code>.</p>
  <p>I tried to annotate <code>Person.born</code> with <code>@Field(targetType = FieldType.IMPLICIT)</code> to tell the system that I want to store it as an object, but it didn't help.</p>