title: How do I configure Jackson Serialization on LocalDateTime and LocalDate for Java?
body: <p>I know that there are many posts on Stackoverflow regarding this topic and I'm sure I've read just about all of them, but I am still struggling to get this working and would appreciate any guidance. </p>
  
  <p>This is the Spring Parent and Jackson Dependencies I am using:</p>
  
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.5.9.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;!-- Jackson --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.module&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-module-parameter-names&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-datatype-jdk8&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Within my Application Configuration I am trying to use: </p>
  
  <pre><code>@Autowired
  void configureJackson(ObjectMapper jackson2ObjectMapper) {
  
      JavaTimeModule timeModule = new JavaTimeModule();
  
      timeModule.addDeserializer(LocalDate.class,
              new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  
      timeModule.addDeserializer(LocalDateTime.class,
              new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  
      timeModule.addSerializer(LocalDate.class,
              new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  
      timeModule.addSerializer(LocalDateTime.class,
              new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  
      jackson2ObjectMapper
              .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
              .setSerializationInclusion(JsonInclude.Include.NON_NULL)
              .registerModule(timeModule)
              .registerModule(new ParameterNamesModule())
              .registerModule(new Jdk8Module())
              .registerModule(new JtsModule());
  }
  </code></pre>
  
  <p>This is the model I am testing with. (I am trying to achive the same in 5-6 models).</p>
  
  <pre><code>public class DateRange implements Serializable {
  
       private static final long serialVersionUID = 6412487507434252330L;
  
       private LocalDateTime startTime;
       private LocalDateTime endTime;
  
       private LocalDate startDate;
       private LocalDate endDate;
  
       // Getters/Setters
  </code></pre>
  
  <p>I am hoping that I could find an approach that would apply globally without the need for me to annotate each field individually. I was using java.util.Date before, but ran into some issues with other functionality there. </p>
  
  <p>Trying to use these newer (better) date models instead of that old damaged one is causing me a lot of headache over a simple thing. </p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I changed my configuration to one of the suggested below and came really close to solving this issue. </p>
  
  <pre><code>@Bean
  @Primary
  public ObjectMapper objectMapper() {
  
      JavaTimeModule timeModule = new JavaTimeModule();
  
      timeModule.addDeserializer(LocalDate.class,
              new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  
      timeModule.addDeserializer(LocalDateTime.class,
              new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  
      timeModule.addSerializer(LocalDate.class,
              new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  
      timeModule.addSerializer(LocalDateTime.class,
              new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  
      return new ObjectMapper()
              //.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
              .setSerializationInclusion(JsonInclude.Include.NON_NULL)
              .registerModule(timeModule)
              .registerModule(new ParameterNamesModule())
              .registerModule(new Jdk8Module())
              .registerModule(new JtsModule());
  }
  </code></pre>
  
  <p>Now the only issue I have is that LocalDateTime is printing timestamps like: "2018-07-26T07:57:12.938" when I need them like: 2018-07-26 07:57:12.</p>
  
  <p>These fields are already in-use today and I need to make this change seamless in a way that doesn't require my API consumers to make any adjustments. </p>
  
  <p><a href="https://stackoverflow.com/users/1426227/cassio-mazzochi-molin%20Cassio">Cassio</a> mentioned that disabling SerializationFeature.WRITE_DATES_AS_TIMESTAMPS will print timestamps in this ISO format, but it's not what I need. I tried to comment out the field in the hopes that it would pickup the custom DateTimeFormatter I am providing, however it has not changed my output.</p>