tl;dr
myTimestamp
.toInstant()
.atOffset(
ZoneOffset.UTC
)
Details
I read a java.sql.Timestamp from an Oracle DB.
When handed an object from the terribly flawed legacy date-time classes, immediately convert to the modern java.time replacement class. That class would be Instant, for representing a moment as seen with an offset fromUTC of zero hours-minutes-seconds.
Conversion methods
To convert, call the new to…/from…/valueOf conversion methods added to the old classes.
Instant instant = myTimestamp.toInstant() ;
The Timestamp needs to be converted to the standard ISO_OFFSET_DATE_TIME as a String.
“converted” is the wrong word to use here. ISO_OFFSET_DATE_TIME refers to a constant holding a predefined formatter. A formatter up is used to parse text to get a date-time object, and is used to generate text representing the content of a date-time object. But a date-time object itself has no “format” as it does not consist of text.
Indeed, you have no need for text at all here in your scenario. As mentioned above, the old legacy class have been gifted with conversion methods.
This String then needs to be parsed into a java.time.OffsetDateTime value.
Apply an offset (ZoneOffset) to your Instant to get a OffsetDateTime.
I assume you want to stick with an offset of zero. So use the constant ZoneOffset.UTC.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
Avoid Timestamp
You can skip this conversion chore by extracting an OffsetDateTime object from your database. No need to ever use Timestamp again in JDBC 4.2+.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
And writing an OffsetDateTime.
myPreparedStatement.setObject( … , odt ) ;