convert XMLGregorianCalendar to java.sql.Timestamp
Asked Answered
C

2

16

I'm trying to assign a XMLGregorianCalendar date to a java.sql.Timestamp var, like this...

var1.setTimeStamp(Timestamp.valueOf(var2.getXMLGregorianCalendar().toString()))

But apparently, this is not working, and throws an exception...

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

And I've tried this, as well:

var1.setTimeStamp((Timestamp) var2.getXMLGregorianCalendar().getTime())

but...

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Timestamp

Any ideas..? Thanks!

Caia answered 20/9, 2012 at 7:58 Comment(0)
C
27

I've found the answer:

    Timestamp timestamp = new Timestamp(var2.getXMLGregorianCalendar().toGregorianCalendar().getTimeInMillis());
    var1.setTimeStamp(timestamp);
Caia answered 20/9, 2012 at 8:40 Comment(0)
P
3

tl;dr

Try to avoid legacy date-time classes. But if handed a javax.xml.datatype.XMLGregorianCalendar, convert to modern java.time.Instant class. No need to ever use java.sql.Timestamp.

myPreparedStatement.setObject( 
    … , 
    myXMLGregorianCalendar  // If forced to work with a `javax.xml.datatype.XMLGregorianCalendar` object rather than a modern java.time class…
    .toGregorianCalendar()  // …convert to a `java.util.GregorianCalendar`, and then…
    .toZonedDateTime()      // …convert to modern `java.time.ZonedDateTime` class.
    .toInstant()            // Adjust to UTC by extracting an `Instant` object.
)

Retrieving from a database, as of JDBC 4.2 and later.

Instant instant = myResultSet.getObject( … , Instant.class ) ;

java.time

FYI, the terribly troublesome old date-time classes have been supplanted by the java.time classes.

Avoid using XMLGregorianCalendar. But if you must interface with old code not yet updated for java.time types, convert. As an intermediate step, convert to GregorianCalendar as seen in the code of your Question.

java.util.GregorianCalendar gc = myXMLGregorianCalendar.toGregorianCalendar() ;

Now use the new convenient conversion method added to the old GregorianCalendar class, to get a modern java.time.ZonedDateTime object.

ZonedDateTime zdt = gc.toZonedDateTime() ;  // Convert from legacy class to modern class.

Adjust from that particular time zone to UTC. Extract an Instant object which is a moment always in UTC, by definition.

Instant instant = zdt.toInstant() ;  // Adjust from some time zone to UTC.

As of JDBC 4.2, we can directly exchange java.time objects with the database. So no need to ever touch java.sql.Timestamp again.

myPreparedStatement.setObject( … , instant ) ;

Retrieval:

Instant instant = myResultSet.getObject( … , Instant.class ) ;

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.

Pythia answered 7/7, 2018 at 22:47 Comment(2)
Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See Is it acceptable to add a duplicate answer to several questions?Roadability
@Roadability I had considered marking this Question and the other Question by the same author as duplicates. But they are not duplicates. This one asks simply about going from XMLGregorianCalendar to java.sql.Timestamp classes .The other asks about a time zone discrepancy in that process. My other Answer gives additional advice about working with UTC to avoid such confusion. My other answer leaves out some details and comments, referring to this Answer for more info.Pythia

© 2022 - 2024 — McMap. All rights reserved.