Avoid legacy date-time classes
You should not be using java.util.Date
. That terrible class was supplanted years ago with the adoption of JSR 310.
java.time
Convert from your legacy XMLGregorianCalendar
to the modern ZonedDateTime
class by way of another terrible legacy class, GregorianCalendar
.
ZonedDateTime zdt = myXmlGregCal.toGregorianCalendar().toZonedDateTime() ;
TIMESTAMP WITH TIME ZONE
column
To write this moment to the database in a column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE
, using JDBC 4.2, convert the ZonedDateTime
to an OffsetDateTime
. Oddly, the JDBC spec requires support for OffsetDateTime
but not Instant
or ZonedDateTime
.
OffsetDateTime odt = zdt.toOffsetDateTime() ;
Pass to your prepared statement.
myPreparedStatement.setObject( … , odt ) ;
Retrieval.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
DATE
column
If writing to a date-only type column, akin to the SQL-standard type DATE
, then use LocalDate
class.
LocalDate ld = zdt.toLocalDate() ; // If the zone of that ZonedDateTime object is the zone by which you want to perceive the date.
myPreparedStatement.setObject( … , ld ) ;
Retrieval.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
