json date format in spring-boot

Tags: ,

I am using spring-boot and I have an entity class defined something like this

import org.joda.time.LocalDateTime;
public class Project {

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
    private LocalDateTime start_date;

When this class is converted to JSON, the field gets converted to the following string representation

{"start_date":[2014,11,15,0,0,0,0],...., ...}

I want to have the json response as yyyy-MM-dd.

I tried the @DateTimeFormat(iso = ISO.DATE) annotation and that did not help either.

Is there an easy way to do this conversion to proper json format ?


There are three things that you need to do to format the date as yyyy-MM-dd:

  1. Add a dependency on com.fasterxml.jackson.datatype:jackson-datatype-joda. Judging by the output you’re getting at the moment, I think you may already have this dependency.
  2. Configure Jackson not to format dates as timestamps by adding spring.jackson.serialization.write-dates-as-timestamps: false to your application.properties file.
  3. Annotate the LocalDataTime field or getter method with @JsonFormat(pattern="yyyy-MM-dd")

Note: You’ll need to use Spring Boot 1.2 for step 2 to work.

Source: stackoverflow