2

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
Thor
  • 6,607
  • 13
  • 62
  • 96

2 Answers2

6

The value should be of type java.util.Date.

So get the Date object from the XMLGregorianCalendar like this:

record.toGregorianCalendar().getTime();

UPDATE:

You can use like this:

<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>

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:

@FacesConverter("com.examples.Date")
public class XMLGregorianCalConverter extends DateTimeConverter {
 private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("GMT");
 private String dateStyle = "default";
 private Locale locale = null;
 private String pattern = null;
 private String timeStyle = "default";
 private TimeZone timeZone = DEFAULT_TIME_ZONE;
 private String type = "date";

@Override
public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    Map<String, Object> attributes = component.getAttributes();
    if(attributes.containsKey("pattern")){
        pattern = (String) attributes.get("pattern");
    }
    setPattern(pattern);
    if(attributes.containsKey("locale")){
        locale = (Locale) attributes.get("locale");
    }
    setLocale(locale);
    if(attributes.containsKey("timeZone")){
        timeZone = (TimeZone) attributes.get("timeZone");
    }
    setTimeZone(timeZone);
    if(attributes.containsKey("dateStyle")){
        dateStyle = (String) attributes.get("dateStyle");
    }
    setDateStyle(dateStyle);
    if(attributes.containsKey("timeStyle")){
        timeStyle = (String) attributes.get("timeStyle");
    }
    setTimeStyle(timeStyle);
    if(attributes.containsKey("type")){
        type = (String) attributes.get("type");
    }
    setType(type);

    XMLGregorianCalendar xmlGregCal = (XMLGregorianCalendar) value;
    Date date = xmlGregCal.toGregorianCalendar().getTime();
    return super.getAsString(context, component, date);
}

}

and use on your page like this:

<h:outputText value="#{bean.value.record}" >
    <f:converter converterId="com.examples.Date" />
    <f:attribute name="pattern" value="dd.MM.yy" />
</h:outputText>

Code inspired/copied from this question: JSF convertDateTime with timezone in datatable

Community
  • 1
  • 1
Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42
  • Yes, it's easy in Java, but how to do this in JSF? – Thor Jul 29 '12 at 18:09
  • Update1 throws a IllegalAccessException. As expected, your alternative is working, but inflexible, because the _pattern_ is hardcoded in the `XMLGregorianCalConverter`. – Thor Jul 29 '12 at 19:16
0

You can register a custom XML Adapter to convert from XMLGregorianCalendar to either Calendar or Date along these lines: How do I customise date/time bindings using JAXWS and APT?

Community
  • 1
  • 1
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • I'm using a provided library which contains the JAXB classes, so an XML Adapter ist not an option. – Thor Jul 29 '12 at 18:08