5

Is there a way to format the date object to be displayed in a select item ?

Here's 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 the combo / selection displays these :

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 like :

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

I wonder how to achieve this ?

Thank you !

Bertie
  • 17,277
  • 45
  • 129
  • 182

4 Answers4

3

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

Adisesha
  • 5,200
  • 1
  • 32
  • 43
  • +1 for the disclaimer. I think there are a lot of answers on SO that would need it. – Matt Handy Apr 13 '11 at 09:43
  • 1
    Another example here: http://stackoverflow.com/questions/2378826/facelets-and-jstl-converting-a-date-to-a-string-for-use-in-a-field/2381443#2381443 – BalusC Apr 13 '11 at 11:31
  • @Adi: Hello .. Thank you for the suggestion, having looked at it, i think it'll work very well. But im curious by the advantages of making it my own EL function over making a simple applicationscoped managed bean with functions to format the date ? – Bertie Apr 13 '11 at 15:20
  • wekcome..You can reuse your EL function in similar senario – Adisesha Apr 13 '11 at 17:48
  • @Adi: And i can reuse my application managed bean to do the formatting as well .. And i can easily add more formatting methods to the jsf bean also. – Bertie Apr 14 '11 at 00:44
  • @Albert Kam May be, but I never tried. I prefer it to be el function because it explicitly says, this is used for presentation only.Try both options and see which one sounds clean to you. – Adisesha Apr 14 '11 at 01:52
  • @Adi: Hello again .. Just want to know about your opinion on the jsf bean solution. Would like to try out both .. Thank you Adi ! – Bertie Apr 14 '11 at 06:57
1

You'd need to employ a date formatter. AFAIK #{periodStartEnd.map['dateStart']} would end up in a toString() call assuming this returns a Date object.

I'm not sure whether Java EL in JSF 2.0 has built in function parameters already, but if not, you could use JBoss EL (extensions to Java EL). With that you could provide a formatter bean and use something like #{formatter.format(periodStartEnd.map['dateStart'], 'dd-MM-yyyy')}

The format would then create a SimpleDateFormat from the format string and return the formatted date as a string.

You could also pass in a locale in order to provide localized formats.

A third alterative would be to directly store the formatted strings in periodStartEnd and access them.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

You can use a converter method in your bean, as:

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

And, in 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')}" />
victorpacheco3107
  • 822
  • 3
  • 10
  • 32
0

You can use f:convertDateTime and specify a pattern.

wjans
  • 10,009
  • 5
  • 32
  • 43
  • That'd be great. Could you please provide an example ? I tried it out when using h:outputText, but im not sure on how to write it for my case above. – Bertie Apr 13 '11 at 09:18
  • Can't you just put it inside your `selectOneMenu` just like you did with the `outputText`? If your select items are dates and you're binding your value to a date, it should work. – wjans Apr 13 '11 at 09:30
  • This runs on item values only, not item labels. Besides, he already has a "genericConverter" on values. – BalusC Apr 13 '11 at 11:32
  • Indeed, my bad, wasn't paying attention to the example, overlooked some important details. Thought it was a matter of formatting dates in the selectOneMenu. Sorry for pointing in the wrong direction here. – wjans Apr 13 '11 at 11:55