I created a table like like this in PostgreSQL:
create table myTable (
dateAdded timestamp(0) without time zone null
Set UTC as default timezone of your JVM -Duser.timezone=UTC
or set your whole OS to UTC.
In Postgres both TIMESTAMP
and TIMESTAMP WITH TIMEZONE
are stored the same way - number of seconds since Postgres epoch (2000-01-01). The main difference is what Postgres do when it saves timestamp value such as 2004-10-19 10:23:54+02
:
+02
is just stripped away -02
correction is performed to make it UTC Now the interesting thing is when JDBC driver loads the value:
In both cases you will end up with java.sql.Timestamp
object with user's default TZ.
Timestamps without TZ are pretty limited. If you have two systems connected to your database, both with different TZ, they will interpret timestamps differently. Therefore, I strongly advice you to use TIMESTAMP WITH TIMEZONE
.
You can tell JDBC what kind of TZ it should use when reading timestamp via ResultSet#getTimestamp(String, Calendar). Excerpt from JavaDoc:
This method uses the given calendar to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information.