Reputation: 65
I need to write to a column of data type Date in postgresql from a java application. In which datatype should I accept the input so that I can use it to insert into Postgresql?
Upvotes: 0
Views: 123
Reputation: 338564
LocalDate
As of JDBC 4.2, the modern way to store a date-only value without time-of-day and without time zone is the LocalDate
class.
LocalDate ld = LocalDate.of( 2019 , Month.JANUARY , 23 ) ;
This maps to a DATE
type in Postgres.
myPreparedStatement.setObject( … , ld ) ;
Retrieval.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
The legacy class java.sql.Date
class is now legacy, and should never be used because of its severe design flaws.
Upvotes: 3