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.

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, I am having facing an issue here. The date displayed in the json format is 1 day less.
Please post what value you are storing and how ? from database or something else
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.
subject.setNumber(subjectContainer.getNumber()); subject.setInitials(subjectContainer.getInitials()); if(subjectContainer.getDateOfBirth() != null) subject.setDateOfBirth(subjectContainer.getDateOfBirth().toGregorianCalendar().getTime());
what is the data type of DateOfBirth field of subjectContainer type object ?
|
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")

6 Comments

Hi, I am having facing an issue here. The date displayed in the json format is 1 day less.
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
Is there a way to do this with all dates, and not have to annotate one by one?
@Pramod did you find any solution for date display 1 day less
It is showing the correct date for me (springboot v.2.0.3)
|
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

1 Comment

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
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());        
        }           
    };
}

2 Comments

Works great! You should add this to your AppConfig/@Configuration class
Is this still working? new ISO8601DateFormat()) seems deprecated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.