I'm trying to add a date to my SQL database (PostgreSQL), but there seems to be a mismatch, and im suspecting that the date is the culprit. Is it possible to save a java.util.Date
in a table where the column is set to DATE?
Asked
Active
Viewed 771 times
0
-
I figure the database is actually PostgreSQL, and the PGAdmin is the name of the tool you're using to administer the DB, right? – hovanessyan Nov 08 '17 at 17:02
-
Yeah, edited it. – Mase Nov 08 '17 at 17:03
-
For a DATE column, you likely need `java.sql.Date` instead `java.util.Date`. See https://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date – david a. Nov 08 '17 at 17:03
-
1how are you persisting the dates in the database (which framework)? – hovanessyan Nov 08 '17 at 17:08
-
in general java.util.date is converted to java.sql.date and this java.sql.Date object type is mapped to a corresponding SQL Date object in the database. Again, in general this should be database vendor agnostic, because they all follow the SQL standard. There are a lot of papers explaining that dates should be saved only in UTF and/or only as BigIntegers (milliseconds) and only converted to real date for displaying purposes (human readable interface). It's a tricky topic (daylight savings, leap seconds, timezones and such). – hovanessyan Nov 08 '17 at 17:16
-
Any reason why you are still using the long outdated `Date` class? [The modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. I recommend you save a modern `java.time.LocalDate` to your date column. – Ole V.V. Nov 08 '17 at 17:31
-
There are more details in [this answer](https://stackoverflow.com/a/38809770/5772882) and in [the PostgreSQL documentation](https://jdbc.postgresql.org/documentation/head/8-date-time.html). – Ole V.V. Nov 08 '17 at 17:36
1 Answers
0
For a DATE you need to use java.sql.Date not java.util.Date

gkgkgkgk
- 707
- 2
- 7
- 26
-
1Even better a `java.time.LocalDate`. Use the modern date and time API wherever you can. – Ole V.V. Nov 08 '17 at 17:32
-
1