I’m consuming a REST webservice and directly using the JAXB objects in my view. One has a date as a XMLGregorianCalendar like this:
@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
While trying to use a standard converter
<h:outputText value="#{bean.value.record}" >
<f:convertDateTime pattern="dd.MM.yy" />
</h:outputText>
I get the error message (translated into english) in my JSF2 environment (JBoss-7.1.1-Final)
javax.faces.convert.ConverterException: fSelection:dtSelection:0:j_idt42:
Converting of '2012-07-25T20:15:00' into string not possible.
It seems, the type XMLGregorianCalendar is not supported by the default converter. I’m wondering if a JSF converter for this date type is available, because this requirement does not seem to be that unusual …
Edit Ravi provided a functional example of a custom converter, but this seems to be to unflexible:
- the pattern is hardcoded
- no support for the user local
The value should be of type java.util.Date.
So get the Date object from the XMLGregorianCalendar like this:
UPDATE:
You can use like this:
This should actually work but since you said you are getting an IllegalAccessException, I am not sure for the exact reason.
Alternatively, you can also write your own converter if you would like to, the converter will look like this:
And if you want to use the same attributes that you would use with a dateTimeConverter, then you need to pass them as attributes to the component and extend DateTimeConverter like this:
and use on your page like this:
Code inspired/copied from this question: JSF convertDateTime with timezone in datatable