Question

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!

Was it helpful?

Solution

I've found the answer:

    Timestamp timestamp = new Timestamp(var2.getXMLGregorianCalendar().toGregorianCalendar().getTimeInMillis());
    var1.setTimeStamp(timestamp);

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top