Jersey JSON Date formatting POJO Mapping - java

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?

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.

Spring JPA Specification-arg-resolver

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.

how to save the datetime field to cosmosdb "SQL API / DocumentDb"?

I am sending the DateTime field from Java to documentdb/SQL API to cosmos db .
but it is saving the datetime as a number in documentdb , if i parse my datetime value to string then documentdb is showing correctly in string form , but i need it in datetime format in documentdb/SQL API .
What's the best practise to save datetime value in documentdb without parsing it to string?
What is the value of the date field in the documentdb collection if i not parse my datetime field to string ? for i.e "loggedAt": 1554207604392 is showing this number in documentdb . Can anyone please help me on this .
Document Db Item, highlighted field is the datetime which i am sending from java
What is the value of the date field in the documentdb collection if i
not parse my datetime field to string ?
The number is representing the number of elapsed seconds since January 1, 1970 which matches your datatime as Unix timestamps. Azure Cosmos DB's internal Timestamp (_ts) property follows this approach. You could refer to this document for details.
What's the best practise to save datetime value in documentdb without
parsing it to string?
From this feedback case,ms doesn't support native datetime type data.In this blog,the datetime data still was treated as String or Number.
As for unix timestamp query operations,You could follow my previous case :Convert TimeScript to Date in Azure CosmosDB SQL Query to convert unix timestamp to datetime by using UDF. Or get some clues from this case:Date Between Query in Cosmos DB

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;

Timestamp getting as long instead of date format

I have a one rest method from Spring3 which returns expected JSON response except date which is in timestamp.
In database date is stored as 2013-08-08 00:30:00. Before sending response to the application, the date I am getting as 2013-08-08 00:30:00. I have checked it in debug mode. But after complete execution of rest-service, I got the date in long format as 1375902000000.
I want to return same timestamp format instead of long format.
I don't want to convert long timestamp to again date format at client side.
By default it is serialized as long if you want to send it like 2013-08-08 00:30:00 you may try asString()
You could do the following to keep your desire date format:
Declare the bean property as String.
Format the date object according to your desire format, example yyyy-dd-MM hh:mm:ss

Categories