pramagouni
pramagouni

Reputation: 1

wrong date(one day less) in hibernate from db

Below is date in DB:

below is code:

List<Date> dataList=new ArrayList<Date>();
query= "SELECT createdTime FROM TableName";
…
…

dataList=selectQuery.list();
for(Object data[] : dataList) {
    System.out.println("Date and time -> "+data[2]); //getting one day less than the original 
}

I am getting 01-01-2018 in result if the date is 02-01-2018.

Upvotes: 0

Views: 1818

Answers (2)

Edson Martins
Edson Martins

Reputation: 47

I changed my entity using LocalDate from java.time instead of using Date from java.util because I had the same issue in my MySQL docker container.

Trying to solve the problem, I changed the configuration from the MySQL config file but got the same result, and then when looking at the website https://thorben-janssen.com/hibernate-jpa-date-and-time/, I found a table comparing the Java type with the JDBC time. Based on the table, I changed from Date to LocalDate and it's working properly now.

Upvotes: 1

user5616998
user5616998

Reputation: 49

the date you got may contain Timezone info, so make sure the DB is running the timezone with your code
or better all in UTC

Upvotes: 0

Related Questions