convert XMLGregorianCalendar to java.sql.Timestamp

elcadro picture elcadro · Sep 20, 2012 · Viewed 30.5k times · Source

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!

Answer

elcadro picture elcadro · Sep 20, 2012

I've found the answer:

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