Home > OS >  Java LocalDate format from reading from JSON and saving to Database
Java LocalDate format from reading from JSON and saving to Database

Time:08-10

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?

CodePudding user response:

  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.

  • Related