How to populate XMLGregorianCalendar ()

I need to populate a JAX Bean from XML, however there is no setter method. I get the following message below

Failed to invoke public javax.xml.datatype.XMLGregorianCalendar() with no args 

I wrote the following methods to take a date and convert it to XMLGregorianCalendar, and then call setter in my wrapper class. However, I still get the exception. Is there a standard way of processing this data type that I am viewing? My wrapper class may not call it, but Netbeans for some reason will not allow me to attach a debugger to it.

 public XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) throws DatatypeConfigurationException { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); if (date == null) { return null; } else { GregorianCalendar gc = new GregorianCalendar(); gc.setTimeInMillis(date.getTime()); return datatypeFactory.newXMLGregorianCalendar(gc); } } 

The installer in Bean is below

 public void setDeliveryDate(XMLGregorianCalendar value) { this.deliveryDate = value; } 
+8
java binding jaxb
source share
2 answers

In your code example, you are trying to populate it with a Date object, while the question itself says you are trying to populate XML. Therefore, if I do not understand, to populate from XML just use:

 XmlGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(yourXmlDateTimeString); 
+8
source share

I would suggest using Joda Time - Java Date Api disappoints many developers. If you want to stick with the core libraries, try using DataTypeFactory.

 public static XMLGregorianCalendar asXMLGregorianCalendar(Date date) { java.util.GregorianCalendar calDate = new java.util.GregorianCalendar(); calDate.setTime(date); javax.xml.datatype.XMLGregorianCalendar calendar = null; try { javax.xml.datatype.DatatypeFactory factory = javax.xml.datatype.DatatypeFactory.newInstance(); calendar = factory.newXMLGregorianCalendar( calDate.get(java.util.GregorianCalendar.YEAR), calDate.get(java.util.GregorianCalendar.MONTH) + 1, calDate.get(java.util.GregorianCalendar.DAY_OF_MONTH), calDate.get(java.util.GregorianCalendar.HOUR_OF_DAY), calDate.get(java.util.GregorianCalendar.MINUTE), calDate.get(java.util.GregorianCalendar.SECOND), calDate.get(java.util.GregorianCalendar.MILLISECOND), 0); } catch (DatatypeConfigurationException dce) { //handle or throw } return calendar; } 
0
source share

All Articles