Date format inside selection element

Is there a way to format the date object that will be displayed in the select element?

Here is my example:

<h:selectOneMenu 
    label="Period" 
    value="#{myBean.periodStartEndList}"
    id="periodStartEnd" 
    converter="genericConverter">

    <f:selectItem itemLabel="Choose one .." noSelectionOption="true" />

    <f:selectItems 
        value="#{myBean.periodStartEndList}" 
        var="periodStartEnd"
        itemValue="#{periodStartEnd}" 
        itemLabel="#{periodStartEnd.map['dateStart']} -- #{periodStartEnd.map['dateEnd']}" />
</h:selectOneMenu>

And combo / selection will display the following:

Sun May 01 14:57:21 WIT 2011 -- Thu May 05 14:57:21 WIT 2011
Fri May 06 14:57:21 WIT 2011 -- Tue May 10 14:57:21 WIT 2011

I would like to have something simpler:

01-05-2011 -- 05-05-2011
06-05-2011 -- 10-05-2011

I wonder how to achieve this?

Thank!

+5
source share
4 answers

You create an EL function for conversion and use it. Check out http://www.javabeat.net/tips/4-expression-language-in-jsp-20.html and http://wiki.apache.org/myfaces/Parameters_In_EL_Functions . Disclaimer: I have never used it and do not know if it works.

+3
source

. AFAIK #{periodStartEnd.map['dateStart']} toString(), Date.

, Java EL JSF 2.0 , , JBoss EL ( Java EL). bean - #{formatter.format(periodStartEnd.map['dateStart'], 'dd-MM-yyyy')}

format SimpleDateFormat .

, .

, periodStartEnd .

+1

f:convertDateTime .

0
source

You can use the bean converter method like:

public class MyBean{
    ...
        public String formatDate(Date fecha, String pattern) {
            return (new SimpleDateFormat(pattern)).format(fecha);
        }
    ...
}

And, on your xhtml page inside f: selectItems:

<f:selectItems 
    value="#{myBean.periodStartEndList}" 
    var="periodStartEnd"
    itemValue="#{periodStartEnd}" 
    itemLabel="#{myBean.formatDate(periodStartEnd.map['dateStart'],'dd-MM-yyyy')} -- #{myBean.formatDate(periodStartEnd.map['dateEnd'],'dd-MM-yyyy')}" />
0
source

All Articles