-3

In as Spring Boot app, I am reading LocalDate from a JSON in dd/MM/yyyy format e.g. 15/03/2009 and while reading, I created them to a request. The format in the request is dd/MM/yyyy.


*ProductRequest:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private LocalDate buyingDate;

And then I save the date fields to database via my Entity. It also has the same format as shown below:


*ProductEntity:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private LocalDate buyingDate;

But the data in the database is shown in yyyy-MM-dd format. I think it is default format for LocalDate, but I need to be clarified about these points: Could you helps me please?

1. Does LocalDate always kept in yyyy-MM-dd format in the database independently from it had in JSON file and in Request?

2. As far as I see, the format while reading the data to ProductRequest is related to the usage when displaying data using this class (ProductRequest). IS that true?

3. Is there any need to use @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") for the date field in ProductEntity? As far as I see, it does not make any sense for the format when saving to database. So, if we do not display or use date field from this entity, then there is no need to use format annotation. Is that true?

1 Answers1

1
  1. Does LocalDate always kept in yyyy-MM-dd format in the database independently from it had in JSON file and in Request?

The JSON representation you use has nothing to do with how data is stored in the database[*]. Each database vendor (Oracle, MySQL, Postgres, SQLServer, etc) has its own way of storing date and date+time values. It's the job of your application or framework (eg, Hibernate) to translate from Java types such as LocalDate into the appropriate format for SQL statements. So the answer is "yes," the format of a date in the application is independent from how it's stored in a database.

  1. As far as I see, the format while reading the data to ProductRequest is related to the usage when displaying data using this class (ProductRequest). IS that true?

Only if you serialize ProductRequest back out to JSON. @JsonFormat applies to both reading (deserializing) and writing (serializing) from/to JSON.

  1. Is there any need to use @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") for the date field in ProductEntity? As far as I see, it does not make any sense for the format when saving to database. So, if we do not display or use date field from this entity, then there is no need to use format annotation. Is that true?

That is correct. In fact, it's very misleading to see that annotation in an entity[*]


[1] Unless you are using a database that natively understands JSON and/or stores data internally as JSON.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Just for clarification: In this case (except from storing the date field to database as JSON [*]), `@JsonFormat()` is only used for displaying date field in a certain format. Except from that, there is no need to use it or format date field in Entity or DTO objects. Is that **ALL** true? –  Aug 09 '22 at 16:34
  • @JsonFormat has nothing to do with "displaying," it is for reading/writing from/to JSON. Whether you "display" that is not relevant. – E-Riz Aug 09 '22 at 16:35
  • Then we should set it based on the date format in JSON. For example, while reading, we set it the same format as the date in JSON file. Similarly, while writing to JSOn, this format define the date format writing to the JSON file. Is that true? –  Aug 09 '22 at 16:38
  • That is correct. – E-Riz Aug 09 '22 at 16:39