My entity has a java.sql.Timestamp
field:
@Column(name = "expires_at")
private Timestamp expiresAt;
The table also has a default column updated_at
, which is dynamically set on insertions/updates (i.e it is not set through JPA):
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
I set expiresAt
in the persisted entity this way:
long currTimestamp = Instant.now().getEpochSecond();
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochSecond(currTimestamp), ZoneId.of("UTC"));
Timestamp expiresAt = Timestamp.valueOf(ldt);
myEntity.setExpiresAt(expiresAt);
I am located in PDT timezone. I run Spring Boot app locally, and it uses MySQL container in my Docker Desktop. This query shows UTC time, which means that MySQL container's timezone is UTC:
select now();
But when I persist my entity into the table, and do select
, the datetime string of expires_at
column shows PDT time. And updated_at
column shows UTC time.
Interestingly, the records persisted from my AWS ECS Spring Boot app into AWS RDS MySQL instance, show UTC for both expires_at
and updated_at
.
When I update local JDBC string (for connecting to Docker Desktop container) to mysql://localhost:3306/mydbname?connectionTimeZone=UTC
, it shows UTC for both.
Can anyone explain why this happens? And why in AWS RDS they are both shown in UTC even though I am not using connectionTimeZone
flag there