0

I use SimpleDateFormat to format a String from a h:inputText field to Date type for an advanced search method.

I tried

if(dateFrom != null && !dateFrom.isEmpty()){
                predicates.add(cb.equal(tt.get(Bean_.dateFrom),
                        cb.parameter(Date.class, "dateFrom")));
            }

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");                     
        if(dateFrom != null && !dateFrom.isEmpty()){               
            query.setParameter("dateFrom", formatter.parse(dateFrom));            
        }

And

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");  
    Date d = formatter.parse(dateFrom);
    formatter.applyPattern("dd-MM-yyyy");        
    if(dateFrom != null && !dateFrom.isEmpty()){               
        query.setParameter("dateFrom", formatter.format(d));            
    }

And other patterns like "yyyy/MM/dd", "MM/dd/yyyy" and "MM-dd-yyyy"

For input field I use p:calendar and I use the same pattern to input date there too. I get the following exception: When I enter 24-02-2014 in the input text.

java.text.ParseException: Unparseable date: "Mon Feb 24 03:00:00 AST 2014"

I looked at all SO quetions regarding this exception and SimpleDateFormat errors but none of them were like mine. It is so weird.

Update
p:calendar:

<p:calendar value="#{beanController.dateFrom}" id="dateFrom" styleClass="dateTimeField" pattern="dd-MM-yyyy">

                                    <p:watermark value="From DD-MM-YYYY" for="dateFrom"/>
                                    <f:param name="dateFrom" value="#{beanController.dateFrom}"/>                                    
                                </p:calendar>
user2911374
  • 159
  • 1
  • 7
  • 16
  • 1
    As the message indicates, the string you're parsing is not `24-02-2014`, but `Mon Feb 24 03:00:00 AST 2014`. No wonder the date format can't parse it, since its pattern is dd-MM-yyyy. The problem is elsewhere. – JB Nizet Feb 22 '14 at 23:54
  • This might help: http://stackoverflow.com/questions/10244164/primefaces-calendar-component-date-conversions – helderdarocha Feb 23 '14 at 00:05
  • @JBNizet thanks for your insight. I looked into the link @helderdarocha provied and deleted `f:convertDateTime`, but it still doesn't work? What might be wrong. I updated my question to include `p:calendar` part. – user2911374 Feb 23 '14 at 00:29
  • If you want "to format a String from a h:inputText" you should include xhtml for h:inputText too. Also, look into http://stackoverflow.com/questions/10699680/mapping-inputtext-to-date-object-in-jsf - a cleaner example of the same thing in the answer – Saulius Šimčikas Feb 23 '14 at 01:10
  • The problem was that primefaces sent the wrong format to the bean. I was binding a String to it, I also tried a java.util.Date type but it didn't work either. I finally could not manage to make primefaces calendar send the correct format to the bean and decided to get rid of it and use jquery datepicker instead. – user2911374 Feb 23 '14 at 23:09

0 Answers0