Spring JPA Specification-arg-resolver - java

I am using the tkaczmarzyk specification-arg-resolver library for filtering data in spring boot jpa. The issue is that client is sending date param in yyyy-MM format but in db I have in yyyy-MM-dd HH:mm:ss format. I want to fetch all the record of that month.
I am looking for a way to fetch records from db of a given month via tkaczmarzyk specification-arg-resolver library by considering that we have date in yyy-MM format as stated above.

Related

Java LocalDate format from reading from JSON and saving to Database

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?
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.
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.
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.

JPA get request shows one day prior the date stored in Database

I am using Spring-Boot for managing my Rest-API. I am storing a Date of birth in my postgreSQL DB as 02/10/1983 using the API with a json payload as "dateOfBirth": "02/10/1983", and Its stored as it is, and the DB result is showing the exact date.
When I am fetching the record using Spring-Boot API call, the date is returned one day prior as 01/10/1983. Every date value is returned a day prior.
I also added a date to insert date using the below code:
long millis=System.currentTimeMillis();
Date currentDate=new Date(millis);
This also saves the date as current date in the DB but returning One day prior the date using rest API get calls.

#JsonFormat Changes the Timestamp value while Serializing the Object

Currently I have a Spring application with some resources that receives different kinds of data. One of the data attributes its a Timestamp and the value is sent in the request. I am using Spring Data Jpa to persist the data in a Postgresql database.
This is how I have my object:
#JsonProperty(value = "control_initial_timestamp")
#Temporal(TemporalType.TIMESTAMP)
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
#Column(name = "ctrl_init_ts")
private Date controlInitTimestamp;
... Setters and Getters ...
My Request looks like this:
"record_insert_timestamp" : "2020-05-18 09:53:24.475"
In the database I receive this: 2020-05-18 05:53:24.475000
If you noticed, it changes the time of the whole timestamp.
Also, with Spring Data, all I am doing is object.save(objectlist); I am not doing any specially query.
Please let me know if I am missing anything.
Thanks,
Is the timezone different between your app server and database server? You can enforce timezone for date serialization in JsonFormat by passing timezone
I got the answer. Not what I expected, but actually how it works.
Databases will store Timestamps as per their timezone location of their remote server location and timezone, to maintain a correlation between all timestamps in the DB/tables/views...etc .
When retrieving the timestamp, if you have configure your current location or timezone or remote server timezone, then it will convert to that specific timezone.
There is no direct way to manipulate this, but its actually how it works.
Thanks,
Let us know what is the datatype defined in database (timestamptz/timestamp)
Refer below link on -- PostgreSQL timestamp
https://www.postgresqltutorial.com/postgresql-timestamp/

Jersey JSON Date formatting POJO Mapping

I have date stored in mysql db as DATETIME and the value is stored in db as
"2016-09-09 15:56:26"
I wanted to display in same format, I am using Jersey 1.19 with POJOMapping and I used Date format in pojo, but the value shown in API is as:
"createdDt":1473454586000
What is the right approach to display correct date format in GET API, should I use String?

Change the format of a date in a Web Service

i have table "news", that has a column called dateNews (type date )
in PostgreSQL the date has the following format : 2014-04-16
but in my jpa web service the format is the following : "2014-04-16T00:00:00+03:00"
how can i format my date to the following format : dd/mm/yyyy
and where should i do it is it in my PostgreSQL database or in my jpa web service ?
This conversion should be done in the service, not at the database level. Make sure your field is type java.sql.Date and use java.text.SimpleDateFormat.
see http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Main reasons to add this at the service level are extensibility and maintainability. You want a single point where you do all this formatting, probably based on user preferences. You don't want to add this to every query you are doing, and maintaining the format throughout those queries.
Use to_char() to output a formated string (text):
SELECT to_char(datenews, 'dd/mm/yyyy') FROM news;

Categories