Primefaces dataTable filtering with date

datatabledatefilterjsf-2primefaces

I came accross a particular problem. I have a datatable in which i want to filter dates in primefaces. when I use

<p:column id="date" headerText="Manufacturing date" 
    filterBy="#{car.dateOfManufacturing}" 
    filterMatchMode="contains">
    <p:outputLabel value="#{car.dateOfManufacturing}"  >
    </p:outputLabel>
</p:column>

Then the filtering of dates happen fine. But when i use

<p:column id="date" headerText="Manufacturing date" 
    filterBy="#{car.dateOfManufacturing}" 
    filterMatchMode="contains">
    <p:outputLabel value="#{car.dateOfManufacturing}"  >
        <f:convertDateTime locale="de"  />
    </p:outputLabel>
</p:column>

the filtering does not happen properly. In fact my observation is with the locale the date format is something like

20.11.2013

but even if I type Wed Nov .. i am able to see filtered results.
I also observed that without locale the date is dispayed as

Wed Nov 20 13:43:37 CET 2013
So i guess it is getting filtered according the latter date even though we see a different date pattern on the screen.

Best Answer

I think need to convert your date before add to filter (filterBy="#{car.dateOfManufacturing}"). One of the simple solution is convert date to string with simple date format in bean.

Here is my code:

My RowData containts: String entry1, String entry2, String dateString, Date date.

My bean method for fill data:

public List<RowData> getTestData() {
  DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
(...)
  entries.add(new RowData("a1", "b1", dateFormat.format(new Date()), currentDate()));
(...)
return entries;

and my XHTML:

<p:column id="date" headerText="Simple date"
                    filterBy="#{entry.dateString}"
                    filterMatchMode="contains">
         <p:outputLabel value="#{entry.dateString}"  >
         </p:outputLabel>
</p:column>

Now I add your code to my table:

<p:column id="dateLocale" headerText="Locale date"
                          filterBy="#{entry.date}"
                          filterMatchMode="contains">
         <p:outputLabel value="#{entry.date}"  >
            <f:convertDateTime locale="de"  />
         </p:outputLabel>
</p:column>

My table:

enter image description here

Filtering works fine with these three date in locale date column. Perhaps, if I start test this issue with many date, the result will be same which described in your answer.

So, I offer covert date to string or use calendar.