Search code examples
jsfoutputformatdatetime

jsf output text with fn substring


I want to format a date in jsf which is xmlgregoriancalendar type. I've come across post which say I need custom converter. Does anybody found solution which does not require custom date converter. I was trying following but I gives me error saying...

Element type "h:outputText" must be followed by either attribute specifications, ">" or "/>".

This is what I tried on jsf

<h:outputText value="#{fn:substringBefore(aClip.lastTransmittedDate,"T")}">
     <f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>

Can anybody point out the explain the error I'm getting?


Solution

  • Apart from your EL syntax error (basically, with that nested double quote you're ending the attribute value too soon that it becomes value="#{fn:substringBefore(aClip.lastTransmittedDate," which is completely wrong EL syntax), this is after all absolutely not the right approach.

    The <f:convertDateTime> converts Date to String and vice versa. It does not convert String in date pattern X to String in date pattern Y at all as you incorrectly seemed to expect.

    Given a XMLGregorianCalendar, you need XMLGregorianCalendar#toGregorianCalendar() to get a concrete java.util.Calendar instance out of it which in turn offers a getTime() method to get a concrete java.util.Date instance out of it, ready for direct usage in <f:convertDateTime>.

    Provided that your environment supports EL 2.2, this should do:

    <h:outputText value="#{aClip.lastTransmittedDate.toGregorianCalendar().time}">
        <f:convertDateTime pattern="dd.MM.yyyy" />
    </h:outputText>
    

    Or, if your environment doesn't support EL 2.2, then change the model in such way that you have for example a getter returning java.util.Calendar:

    public Calendar getLastTransmittedDateAsCalendar() {
        return lastTransmittedDate.toGregorianCalendar();
    }
    

    so that you can use

    <h:outputText value="#{aClip.lastTransmittedDateAsCalendar.time}">
        <f:convertDateTime pattern="dd.MM.yyyy" />
    </h:outputText>
    

    You can even add a getter returning the concrete java.util.Date.