Title: How can I get my date time in correct format while getting from database in spring and hibernate?
Tags: java;spring;hibernate;rest
Question: I am using spring and hibernate to develop an application.I have used
@Entity
public class Category {
@CreationTimestamp
private java.sql.Timestamp created;
//getter and setter
for automatically saving the created time.It saves the created time well in  database like:
2017-03-29 12:19:16
but when I get the json object my date is changed to another format like
"created": 1490769256000
My method for getting a category is:
public Category findCategoryById(int id) throws SQLException {
session = sessionFactory.getCurrentSession();
return ((Category)session.get(Category.class,id));
How can I get it in the same format like it is saved in the database.
Comment: Cant I get date in simple date format while retrieving from database using above method?

Comment: `1490769256000` millseconds since 1970/01/01 Can create a `Date` Object, can use SimpleDateFormat to format

Comment: Yes jdbc allows you to get a value as a Date, but where does the json come from?

Comment: Possible duplicate of [How to convert a timestamp in JSON into normal date and time in java?](http://stackoverflow.com/questions/30994522/how-to-convert-a-timestamp-in-json-into-normal-date-and-time-in-java)


Here is another answer: import java.sql.Date;
import java.text.SimpleDateFormat;
public class MainClass {
public static void main(String[] args) {
long long_date = 1490769256000L;
Date dt = new Date (long_date);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
System.out.println(sf.format(dt));
check main method, by this way you can

Here is another answer: You can use the new java.time classes of JDK8 to convert as below:
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(1490769256000L), ZoneId.systemDefault());
System.out.println(ldt.format(DateTimeFormatter.ISO_DATE_TIME));

Here is another answer: You could take advantage of Java's @JsonFormat Annotation.
In this case, it would look like
@CreationTimestamp
//Json Format for your date here
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
private java.sql.Timestamp created;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:05/02/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><spring><hibernate><rest>', 'Title': 'How can I get my date time in correct format while getting from database in spring and hibernate?', 'ViewCount': 1127, 'id': 43731248, 'user_AboutMe': None, 'user_AccountId': 9641684.0, 'user_DisplayName': 'pravin', 'user_Reputation': 51.0, 'user_Views': 13.0, 'user_id': 7155702.0}}