0

I have a table User with columns

user_id, first_name,last_name, created_time(timestamp)

I have a class User Entity

@Getter
@Setter
@Table(name="user")
@Entity
public class User {
   @Id
   private Long userId;
   @Column(name="first_name")
   private String firstName;
   @Column(name="last_name")
   private String lastName;
   @Column(name="created_time")
   private Timestamp createdTime;
}

I have an interface User Repository

public interface UserRepository extends CRUDRepository<User,Long> {
   User findByUserId(Long id);
}

The created_time stored in database table is 2020-09-08 15:38:13 and when i read the object using spring data jpa it returned as "2020-09-08 21:08:13"

How should i ensure that this automatic time zone conversion not to happen?

2 Answers 2

1

The root cause of the problem is that Jackson automatically converts the timestamp values to UTC and then serializes the same.

In order to correct this behavior, you can add following property to your application.properties and specify the same timezone value as is being used by your DB server.

spring.jackson.time-zone=Asia/Kolkata
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the same with a dummy project and it worked for me. Can you please share the versions of different components involved.
Mysql 5.7.3, Spring boot 2.3.0.RELEASE, mysql driver 8.0.20
0

There is an article that explains this problem and also proposes solutions.

You can also have a look at this answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.