Date format in the json output using spring boot
Asked Answered
A

4

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.

Ark answered 13/3, 2015 at 8:8 Comment(0)
P
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.

Progestational answered 13/3, 2015 at 9:45 Comment(8)
Hi, I am having facing an issue here. The date displayed in the json format is 1 day less.Ark
Please post what value you are storing and how ? from database or something elseProgestational
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.Ark
subject.setNumber(subjectContainer.getNumber()); subject.setInitials(subjectContainer.getInitials()); if(subjectContainer.getDateOfBirth() != null) subject.setDateOfBirth(subjectContainer.getDateOfBirth().toGregorianCalendar().getTime());Ark
what is the data type of DateOfBirth field of subjectContainer type object ?Progestational
@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.Alleluia
@Alleluia yes its working for me also. I have used similar things in my project.Progestational
@Ark adding timezone to @JsonFormat shall fix the problem.Nowicki
G
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")

Gery answered 13/3, 2015 at 9:30 Comment(6)
Hi, I am having facing an issue here. The date displayed in the json format is 1 day less.Ark
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 annotationSewel
Is there a way to do this with all dates, and not have to annotate one by one?Axil
@Ark did you find any solution for date display 1 day lessDiscontent
It is showing the correct date for me (springboot v.2.0.3)Rhetor
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.Torquay
N
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

Numeral answered 23/10, 2017 at 13:59 Comment(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.ymlUniform
H
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());        
        }           
    };
}
Hiett answered 9/5, 2017 at 5:45 Comment(2)
Works great! You should add this to your AppConfig/@Configuration classNabors
Is this still working? new ISO8601DateFormat()) seems deprecatedIncestuous

© 2022 - 2024 — McMap. All rights reserved.