I'm currently building a gateway using Spring-Boot that reads data from a PSQL database. The PSQL database v11.3
is populated by a Rails web app, running Rails 5.1.3
. The Spring app is using Java 8
and Spring Data JPA.
The relevant query is run by a scheduled method in the Spring app, that compares the current time against the updated_at
times of the table. The query is as follows:
@Query("SELECT d from SimulatedDeviceEntity d WHERE d.updatedAt > :currentTime")
List<SimulatedDeviceEntity> findByLastUpdated(@Param("currentTime") Timestamp currentTime);
If I retrieve a SimulatedDeviceEntity
from this query and get its time from getUpdatedAt
, it is of type java.sql.Timestamp
. When displayed using toString()
I get 2019-06-26 17:24:20.034923
for the device that was updated in my local time at 13:24
.
I've tried a variety of answers on how to generate a Timestamp in UTC, including here where my code to create the Timestamp is
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("GMT"));
Timestamp currentTime = Timestamp.valueOf(zdt.toLocalDateTime());
Regardless of what I try, calling toString
on any of these will generate the Timestamp in my local time, which is 4 hours behind UTC. I've come to understand that this is intentional, however when calling toString
on the Timestamps retrieved from the Rails app, even then they display in UTC.
Given that toString
should display the Timestamp in local time, the fact that it displays for my last_updated
times in UTC leads me to believe something else is at play here.