I am working with a PostgreSQL database , in which I have a type field timestamp
since I only want a date with its time (no time zone).
To do the saving I am making use of the LocalDateTime
Java 8 object.
The question is, is it good practice to do it using that object, or should we use the object Date
from the java.sql package?
Let's review the options:
java.sql.Date
, as its name indicates, only saves the date, but without the time. We should not confuse it withjava.util.Date
.java.sql.Timestamp
extendsjava.util.Date
, making it more precise when saving down to nanoseconds.So the pre-Java 8 option would be
java.sql.Timestamp
.With Java 8 or higher:
The PostgreSQL documentation points to the use of LocalDate (for the DATE type) and LocalDateTime (for the TIMESTAMP type):
Because you do not use the Timestamp class that is also in Java, it would be the most correct
https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html