Avoid legacy classes
You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use either Date
class, nor Timestamp
.
Furthermore, apparently your database is storing a date-only. So both java.util.Date
and java.sql.Timestamp
are misfits. Those represent a moment, a specific point on the timeline, not just a date.
DATE
column?
You neglected to tell us the data type of your column. But I would guess that the column is of Postgres type DATE
, given that you claim to be getting only year-month-day without any time-of-day.
The DATE
type in Postgres is akin to the DATE
type specified in the SQL standard.
java.time.LocalDate
For a column of a type akin to the SQL standard type DATE
, the matching Java class mapped in JDBC 4.2+ is java.time.LocalDate
.
Write.
LocalDate ld = LocalDate.of( 2023 , Month.JANUARY , 23 ) ;
myPreparedStatement.setObject( … , ld ) ;
Retrieve.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;