tl;dr
You have the wrong data type in MySQL, and the wrong class in Java.
Example code snippet.
myPreparedStatement
.setObject(
… , // Specify which placeholder `?` to fill-in.
OffsetDateTime // JDBC 4.2 and later requires a JDBC driver support exchanging `java.time.OffsetDateTime` objects with the database.
.now( ZoneOffset.UTC ) // Capture the current moment as seen in UTC (an offset of zero hours-minutes-seconds).
)
Wrong data type in MySQL
The assumption is that the timestamp is in UTC
Do you understand that DATETIME
in MySQL is not in UTC? That type in MySQL has no concept of time zone nor offset-from-UTC.
If you are tracking moments, specific points on the timeline, you have the wrong data type in MySQL. You should be using TIMESTAMP
to track moments in MySQL.
Wrong Java classes
You are using terrible date-time classes that were bundled with the earliest versions of Java. Those classes were supplanted by the modern java.time classes in Java 8 and later, as defined in JSR 310. Never use the java.sql.Timestamp
class.
To get the current moment in UTC, use Instant
.
Instant instant = Instant.now() ;
Define your column in MySQL as type TIMESTAMP
.
You may be able to write an Instant
via your JDBC driver. However, JDBC 4.2 requires support for OffsetDateTime
but oddly omits required support for Instant
. No matter, we can easily convert.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
Write that to the database using your JDBC driver compliant with JDBC 4.2 or later, through a prepared statement.
myPreparedStatement.setObject( … , odt ) ;
Retrieval.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
Time zone problem eliminated
If you follow the code seen here you will have no time zone problems.
- None of this code depends on the JVM’s current default time zone.
- None of this code depends on the database server’s current default time zone.

About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?