question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonFormat does not affect serialisation of Date

See original GitHub issue

i use spring boot 1.3.0 (tried to 1.3.0.M5), spring-data-jpa, spring rest and mysql. i use java 8.

In my dto i have

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
private Date birthdate;

client still continue to receive yyyy-mm-dd

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
marccollincommented, Oct 1, 2015

if i use @Temporal(javax.persistence.TemporalType.TIMESTAMP)

that work.

0reactions
wilkinsonacommented, Oct 1, 2015

This doesn’t have anything to do with Spring Boot. You’re returning a java.sql.Date and Jackson doesn’t appear to apply the specified formatting to it. You can see the problem with the following code that doesn’t involve Boot at all:

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(new Foo(new java.util.Date())));
        System.out.println(objectMapper.writeValueAsString(new Foo(new java.sql.Date(System.currentTimeMillis()))));
    }

    static class Foo {

        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
        private Date birthdate;

        public Foo() {

        }

        public Foo(Date birthdate) {
            this.birthdate = birthdate;
        }

        public Date getBirthdate() {
            return birthdate;
        }

    }

It produces the following output:

{"birthdate":"01/10/2015"}
{"birthdate":"2015-10-01"}

Note that the Jackson documentation has this to say about java.sql.Date:

Notes on java.sql.Date

(aka “Please do NOT use java.sql.Date, ever!”)

Although Jackson supports java.sql.Date, there are known issues with respect to timezone handling, partly due to design of this class. It is recommended that this type is avoided, if possible, and regular java.util.Date (or java.util.Calendar) used instead. If this is not possible, it may be necessary for applications to convert these dates using java.util.Calendar and explicit timezone definition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonFormat deserialization fails - jackson - Stack Overflow
No, the @JsonFormat annotation is applicable for both serailization and deserailization. So in de-serailization it is used to parse the datetime string to ......
Read more >
Guide to @JsonFormat in Jackson - Baeldung
We'll now use @JsonFormat to specify the format to serialize the createdDate field. Let's look at the User class updated for this change....
Read more >
Annotation Type JsonFormat
General-purpose annotation used for configuring details of how values of properties are to be serialized. Unlike most other Jackson annotations, ...
Read more >
Serialization Attributes - Json.NET
Attributes can be used to control how Json. ... DateTime LastModified { get; set; } // not serialized because mode is opt-in public...
Read more >
JsonFormat.Feature (Jackson-annotations 2.12.0 API)
Enclosing class: JsonFormat. public static enum JsonFormat.Feature extends Enum<JsonFormat ... Only affects deserialization, has no effect on serialization.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found