38

I am working on spring boot for creating a REST application. And I have a DTO as shown below:

public class Subject {

private String uid;
private String number;
private String initials;
private Date dateOfBirth;

And I use Spring-Hateos and the reurn type of my controller is ResponseEntity<Resources<Resource<Subject>>>. I need the date to be displayed in the "yyyy-mm-dd" format.

asgs
  • 3,928
  • 6
  • 39
  • 54
Pramod
  • 731
  • 2
  • 10
  • 24

4 Answers4

68

If you have Jackson integeration with your application to serialize your bean to JSON format, then you can use Jackson anotation @JsonFormat to format your date to specified format.
In your case if you need your date into yyyy-MM-dd format you need to specify @JsonFormat above your field on which you want to apply this format.

For Example :

public class Subject {

     private String uid;
     private String number;
     private String initials;

     @JsonFormat(pattern="yyyy-MM-dd")
     private Date dateOfBirth;  

     //Other Code  

}  

From Docs :

annotation used for configuring details of how values of properties are to be serialized.

More Reference Doc

Hope this helps.

vivekmore
  • 403
  • 5
  • 19
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
  • 4
    Hi, I am having facing an issue here. The date displayed in the json format is 1 day less. – Pramod Mar 13 '15 at 11:40
  • Please post what value you are storing and how ? from database or something else – Yagnesh Agola Mar 13 '15 at 12:01
  • I am retrieving the date from an soap service which is in XMLGregorianCalendar format. I have converted this into java.util.Date, and it is converted to json format. For ex: after converting XMLGregorianCalendar object to Date, it displays 2014-02-11 in the console. But in the json output it is displaying 2014-02-10. – Pramod Mar 13 '15 at 12:15
  • subject.setNumber(subjectContainer.getNumber()); subject.setInitials(subjectContainer.getInitials()); if(subjectContainer.getDateOfBirth() != null) subject.setDateOfBirth(subjectContainer.getDateOfBirth().toGregorianCalendar().getTime()); – Pramod Mar 13 '15 at 14:44
  • what is the data type of `DateOfBirth` field of subjectContainer type object ? – Yagnesh Agola Mar 17 '15 at 12:09
  • @YagneshAgola , Does this work with Deserialization too? I tried this for a similar problem, but it is not working : I am trying to deserialize a Json response from a web service using jackson. – bub Aug 16 '16 at 11:16
  • @bub yes its working for me also. I have used similar things in my project. – Yagnesh Agola Aug 17 '16 at 04:13
  • @Pramod adding `timezone` to `@JsonFormat` shall fix the problem. – Waleed Abdalmajeed Jul 17 '18 at 11:50
32

You most likely mean "yyyy-MM-dd" small latter 'm' would imply minutes section.

You should do two things

  • add spring.jackson.serialization.write-dates-as-timestamps:false in your application.properties this will disable converting dates to timestamps and instead use a ISO-8601 compliant format

  • You can than customize the format by annotating the getter method of you dateOfBirth property with @JsonFormat(pattern="yyyy-MM-dd")

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • 2
    Hi, I am having facing an issue here. The date displayed in the json format is 1 day less. – Pramod Mar 13 '15 at 11:40
  • 4
    Tested with Spring Boot 1.3.0 you do not seem to require the ```spring.jackson.serialization.write-dates-as-timestamps``` but it is sufficient to only use the ```@JsonFormat``` annotation – ngeek Nov 25 '15 at 21:41
  • 7
    Is there a way to do this with all dates, and not have to annotate one by one? – ephemeralCoder Jun 09 '16 at 15:48
  • 1
    @Pramod did you find any solution for date display 1 day less – Varun Chawla Dec 07 '17 at 12:05
  • It is showing the correct date for me (springboot v.2.0.3) – biniam Sep 19 '18 at 09:44
  • Facing the same issue here too, am using spring-boot 2.1.8.RELEASE. Date one day less. Note: in table am just storing till date:2019-09-23 not datetime:2019-09-23 10:01:00 does this makes any difference. – 786543214 Jan 25 '20 at 15:15
12

Starting from Spring Boot version 1.2.0.RELEASE , there is a property you can add to your application.properties to set a default date format to all of your classes spring.jackson.date-format.

For your date format example, you would add this line to your properties file:

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

Reference https://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/html/common-application-properties.html

Daniel Higueras
  • 2,404
  • 22
  • 34
  • 1
    If it is a property, I believe it should be imported somewhere. The question is Where. Tested - does not work if simply added to application.properties or application.yml – Andrey M. Stepanov Jan 17 '19 at 14:47
3

If you want to change the format for all dates you can add a builder customizer. Here is an example of a bean that converts dates to ISO 8601:

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.dateFormat(new ISO8601DateFormat());        
        }           
    };
}
Willem
  • 917
  • 7
  • 19