25

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

import org.joda.time.LocalDateTime;
@Entity
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 ?

Thomas Traude
  • 840
  • 7
  • 17
Parag Sanghavi
  • 283
  • 1
  • 4
  • 6

4 Answers4

36

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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Thank you. I had to upgrade to spring 1.2 and then added the property in the application.yaml file and it worked for me. No @JsonFormat annotation was needed – Parag Sanghavi Dec 19 '14 at 08:37
  • Which property did you have to add in application.yaml to change the date format? – Maksim Aug 24 '15 at 06:20
  • Step 3 appears to be unnecessary (that is the default format). AND actually appears to be ineffectual if you try any other formats. I'm still investigating but it doesn't seem to be even looking at the JsonFormat annotation – rewolf Mar 10 '17 at 01:02
7

Without additional dependency - the only thing I had to do is:

  1. To take care send date from client as string object, in format yyyy/MM/dd

  2. In Spring Boot application, to add annotation on the date field with the same format


public class Foo
{
     @JsonFormat(pattern = "yyyy/MM/dd")
     private Date dueDate;
}

Using Spring Boot 2.3.5 version


Update

Another option, instead of step 2, to modify application.properties file, add there the format for any Date object:

spring.jackson.date-format=yyyy/MM/dd

Adir Dayan
  • 1,308
  • 13
  • 21
1

You can use @JsonFormat annotation in and the desired pattern like this without using any dependency :

@JsonFormat(pattern="yyyy-MM-dd")
private Date created_At;
Arjun Gautam
  • 321
  • 2
  • 5
0

Took me some time struggling with Spring Boot Application + Date Format for my input so I'll try to resume what I saw.

  1. If your date is argument to a function, you can use @DateTimeFormat(pattern = "yyyy-MM-dd") to define a pattern (ie. org.springframework.format.annotation.DateTimeFormat).

  2. If your date is inside an object argument to the function, you can use @JsonFormat(pattern = "yyyy-MM-dd") to define a pattern (ie. com.fasterxml.jackson.annotation.JsonFormat)

  3. If neither of these works, you can try changing your date Type, for me I had tu use org.joda.time.LocalDate in order to make it work with option 2 :

    @JsonFormat(pattern = "dd/MM/yyyy")
    private org.joda.time.LocalDate date;
Alexandre Hamon
  • 1,162
  • 12
  • 13