I am providing the modern answer.
java.time and JDBC 4.2
You should avoid the Timestamp
class. It’s poorly designed and very confusing, a true hack on top of the already poorly designed java.util.Date
class. The fact that the other answers lead to different results as documented by the comparisons in the answer by rve in my opinion illustrates the confusion very well. You are already using OffsetDateTime
from java.time, the modern Java date and time API, and provided that you have got a JDBC 4.2 compliant JDBC driver, you can and should stick to the classes from java.time.
Best to store as timestamp with time zone
Storing dates and times in UTC in the database as you say you want is a good and recommended practice. If you can, change the datatype in the database to timestamp with time zone
. While this doesn’t store a time zone (despite the name), it makes sure that the database too “knows” that timestamps are in UTC, which already prevents many mistakes. Next advantage is that (provided that I have understood correctly) you can store your OffsetDateTime
directly and let the conversion to UTC happen automatically.
OffsetDateTime odt = OffsetDateTime.of(
2015, 6, 4, 19, 15, 43, 210987000, ZoneOffset.ofHours(1));
PreparedStatement stmt = yourDbConnection.prepareStatement(
"insert into your_table (your_timestamp_with_time_zone) values (?);");
stmt.setObject(1, odt);
stmt.executeUpdate();
If you want to make it clearer in your Java code that the time is stored in UTC, convert explicitly first:
odt = odt.withOffsetSameInstant(ZoneOffset.UTC);
If your database stores timestamp
without time zone
If the datatype in your database is a mere timestamp
(without time zone) (not recommended), the type to use on the Java side is LocalDateTime
. I’d do the conversion to UTC like this:
LocalDateTime ldt = odt.withOffsetSameInstant(ZoneOffset.UTC).toLocalDateTime();
System.out.println("UTC datetime = " + ldt);
Output is:
UTC datetime = 2015-06-04T18:15:43.210987
Storing into the database is similar to before:
PreparedStatement stmt = yourDbConnection.prepareStatement(
"insert into your_table (your_timestamp) values (?);");
stmt.setObject(1, ldt);