2

i have select a SelectOneMenu in my jsf page but display date like

Thu May 30 00:00:00 WET 2013

but i want display it like 30/06/2013 in the jsf page. thats my code in my jsf page:

<p:selectOneMenu id="Dateplanif" value="#{ gestionduplanning.dateplanificationnew}"> 
      <f:selectItems value="#{gestionduplanning.datelist}" var="da" itemValue="#{da}" itemLabel="#{da}"  />  
 </p:selectOneMenu> 

i am showing i list of date in jsf that the user need to chose one of them but i want to sow it like dd/MM/yyyy .

this a part of my managed bean :

List<Date> datelist = new ArrayList<Date>();
Date dateplanificationnew ;

public void initialize() {
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
     datelist = manageOfPlanifie.retournerdatedesplanif();
      }  

how i can display it like dd/MM/yyyy ??

update 1 :

i used the solution of @mekk work but i cant validate and get the value of date after thats what i got enter image description here

how to solve that ??

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
marouanoviche
  • 253
  • 4
  • 11
  • 28

7 Answers7

3

You need to convert the item labels of <h:selectOneMenu>. The <f:convertDateTime> is not applicable as it applies to the item value only, not to the item label. The label is in your case just presented using the default Date#toString() pattern which is indeed in format Thu May 30 00:00:00 WET 2013 as described in javadoc.

Your best bet is either creating a List<SelectItem> instead of List<Date> wherein you convert the item labels yourself,

List<Date> availableDates = ...;
List<SelectItem> selectItems = ...;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

for (Date availableDate : availableDates) {
    selectItems.add(new SelectItem(availableDate, sdf.format(availableDate)));
}

or using <f:selectItems var> with an EL function in the itemValue. For example, the OmniFaces of:formatDate() (or homegrow one).

<f:selectItems value="#{bean.availableDates}" var="date" 
    itemValue="#{date}" itemLabel="#{of:formatDate(date, 'yyyy/MM/dd')}" />

This problem is by the way not specific to <p:selectOneMenu>, you would have exactly the same problem when using the standard <h:selectOneMenu>.

Please note that you still need <f:convertDateTime> for the item values, otherwise you may end up in Validation Error: Value is not valid error because of time differences: JSF 2 - f:selectItems with a Date keyed Map.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

try adding this:

convertDateTime pattern="yyyy/MM/dd" 

after your selectItems . I'm not sure if this works on your case but it terms of date, aside from creating EL function conversion, Im using this one.

Mehran
  • 1,409
  • 2
  • 17
  • 27
van
  • 139
  • 2
1
<h:body>
    <h:form>
        <p:selectOneMenu>
            <f:selectItems value="#{test.dates}" itemValue="#{currentDate}" var="currentDate"  itemLabel="#test.formatDate(currentDate)}" />
        </p:selectOneMenu>
    </h:form>
</h:body>

and define formatDate() as

public static String formatDate(Date currentDate) {
     DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
     return dateFormat.format(currentDate);
}
makata
  • 2,188
  • 2
  • 28
  • 23
  • i need now to fixe a new error that i have i made new post then i will continue to see with converstio http://stackoverflow.com/questions/16964863/selectonemenu-showen-red-when-have-date-value – marouanoviche Jun 06 '13 at 14:34
1

i found a solution to my problem with converting my date to String then back it to Date . i will show you a sample example of it :

this is my managed bean:

public class Testbean {
    @EJB
    private ManageOfPlanifieLocal manageOfPlanifie;
    List<Date> listdate = new ArrayList<Date>();
    List<String> listdatestring = new ArrayList<String>();
    String newdate="";
    Date dateplanification;     
 @PostConstruct
    public void initialize() {
        listdate=manageOfPlanifie.retournerdatedesplanif();
        Format formatter = new SimpleDateFormat("dd/MM/yyyy");     
        for(int i=0;i<listdate.size();i++)
        {listdatestring.add(formatter.format(listdate.get(i)));}     
    }  
    public String gototest2(String datedate) throws ParseException  
    {dateplanification= new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(datedate);
        return "test2.xhtml?faces-redirect=true";
    }

test1.xhtml (my first jsf page)

<h2>Choix de l'equipe</h2>  
         <h:outputLabel for="dateplanif" value="date de planification : " />
         <p:selectOneMenu id="dateplanif"  value="#{testbean.newdate}" >             
             <f:selectItems value="#{testbean.listdatestring}" var="da" itemValue="#{da}" itemLabel="#{da}" />  
    </p:selectOneMenu>           

      <p:commandButton value="suivant"  style="color:black;" action="#{testbean.gototest2(testbean.newdate)}" update="@form" />

test2.xhtml

 <h:outputText value="Date : "/>   
 <h:outputText value="#{testbean.dateplanification}" />   

this solution is working very well for me and escape problem in selectOneMenu .

marouanoviche
  • 253
  • 4
  • 11
  • 28
1

Use OmniFaces #{of:formatDate()} function. It will work like a charm. And OmniFaces can combine with PrimeFaces or ADF.

Below is sample code in my application.

<p:selectOneMenu value="#{periodId}">
   <f:selectItem itemLabel="#{appmsg['global.please_choose']}" itemValue=""/>
   <f:selectItems value="#{listPeriod}" var="p" itemValue="#{p.id}"
       itemLabel="#{of:formatDate(p.startDate, 'dd MMMM yyyy')" />
</p:selectOneMenu>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rizkykojek
  • 11
  • 1
0

why don't you change the date pattern at the backing bean, before populating it in the <p:selectOneMenu>? That would be easier.

<h:body>
    <h:form>
        <p:selectOneMenu>
            <f:selectItems value="#{test.dates}"/>
        </p:selectOneMenu>
    </h:form>
</h:body>

And the Bean like this:

private List<String> dates;

public List<String> getDates() {
    return dates;
}

public void setDates(List<String> dates) {
    this.dates = dates;
}
public Test() {
    dates = dateList();
}
public List<String> dateList(){

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    List<String> list = new ArrayList<String>();
    Date date1 = new Date();
    Date date2 = new Date(2013, 01, 01);
    list.add(dateFormat.format(date1));
    list.add(dateFormat.format(date2));
    return list;
}

See if this helps.

NDeveloper
  • 3,115
  • 7
  • 23
  • 34
0

You can use omnifaces. There are sample following link. http://showcase.omnifaces.org/functions/Dates

Zapateus
  • 516
  • 3
  • 8
  • 21