Why is my Java LocalDate being saved as the Previous Day inside Postgres?

118 Views Asked by At

Summary

If I create a Java LocalDate object (ex: "2023-09-28") and save it to my Postgres db the previous day gets stored (ex: "2023-09-27").

  • The stored database value is always 1 day behind

Code

Here is my code (micronaut):

@MappedEntity("event")
data class Event(

    @field:Id val id: UUID,

    val date: LocalDate // <===
)

@JdbcRepository(dialect = Dialect.POSTGRES)
interface EventRepository: CrudRepository<Event, UUID> {}

Here is how I save it:

    val date = LocalDate.parse("2023-09-28", DateTimeFormatter.ISO_LOCAL_DATE)

    val newEvent = Event(id, date)
    repo.save(newEvent)

The logs show the right values being sent:

DEBUG io.micronaut.data.query - Executing SQL query: INSERT INTO "event" ("date","id") VALUES (?,?)
TRACE io.micronaut.data.query - Binding parameter at position 1 to value 2023-09-28 with data type: DATE

Database

I'm using this driver: "org.postgresql:postgresql"

My postges table is using column type date:

create table event
(
    id   uuid not null,
    date date not null
);

Runnable Example

I have a minimal reproducible example here:

Research

Could possibly be related to this?

"LocalDate conversion should ignore timezones"

0

There are 0 best solutions below