The Answer by Stephen C is correct.
Avoid the troublesome old date-time classes such as java.sql.Timestamp
. Now supplanted by the java.time classes.
You have a string in standard ISO 8601 format, representing a date-time with an offset-from-UTC but no indication of time zone. So parse as an OffsetDateTime
. Move into UTC as a Instant
for storage.
Upon retrieval, assign any time zone you wish.
String input = "2017-08-01T15:15:03.313456768+02:00";
OffsetDateTime odt = OffsetDateTime.parse( input ) ;
Instant instant = odt.toInstant() ; // Adjust into UTC for storage.
myPreparedStatement.setObject( … , instant ) ;
Retrieve.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZoneId z = ZoneId.of( "America/Montreal" ) ; // or "Europe/Berlin", "Asia/Kolkata", whatever.
ZonedDateTime zdt = instant.atZone( z ) ; // Move from UTC to a 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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.