65

If #{myBean.birthdate} is of java.util.Calendar or java.util.Date type, can I possibly format this inside the EL itself using an existing function perhaps, with the output of like the one produced by the DateFormat's SHORT, MEDIUM,LONG abd FULL output type?

Instead of outputting the complete form for the #{myBean.birthdate}: Wed Jan 19 19:01:42 WIT 2011, I just prefer a simple output of Jan 19, 2011.

Should I use #{formatBean.format(myBean.birthdate)} instead?

3 Answers 3

173

Use <f:convertDateTime>. You can nest this in any input and output component. Pattern rules are same as java.text.SimpleDateFormat.

<h:outputText value="#{someBean.dateField}" >
    <f:convertDateTime pattern="dd.MM.yyyy HH:mm" />
</h:outputText>
6
  • life.java - Jigar Joshi Wow, this is something new for me, very nice ! Thank you ! But what about this following case ? Im using primefaces, and the date value is to be embedded in the attribute value. For example, <p:panel header="searching data for #{myBean.dateField}" ... />.
    – Bertie
    Jan 19, 2011 at 12:18
  • @bertie: see my answer below and use OmniFaces. Dec 16, 2013 at 13:31
  • how is it possible to check the year or month for example ? Nov 4, 2016 at 11:28
  • jmj and @BalusC: Do I understand it correctly that f:convertDateTime is using the timezone OF THE SERVER which may be different from the timezone of THE USER/CLIENT, and thus it can be a DIFFERENT DATE on the computer of the client and the computer of the server, i.e. we end up with WRONG value of type java.util.Date, which is representing a point in time, not a Day-Month-Year triple as is usually the intent of specifying a Date in a web form, please?
    – Palo
    Jul 13, 2022 at 18:03
  • @Palo - it was more than 11 year ago. i don't remember this. It might have changed over time. I don't have a quick setup to test this. You can test this by placing server in different timezone (UTC in docker) and test this out - if you have the setup.
    – jmj
    Jul 13, 2022 at 18:44
27

If you use OmniFaces you can also use it's EL functions like of:formatDate() to format Date objects. You would use it like this:

<h:outputText value="#{of:formatDate(someBean.dateField, 'dd.MM.yyyy HH:mm')}" />

This way you can not only use it for output but also to pass it on to other JSF components.

1
  • 5
    With OmniFaces approach it's more intuitive than the f:convertDateTime.
    – Stephan
    Jan 31, 2014 at 14:35
7

With EL 2 (Expression Language 2) you can use this type of construct for your question:

    #{formatBean.format(myBean.birthdate)}

Or you can add an alternate getter in your bean resulting in

    #{myBean.birthdateString}

where getBirthdateString returns the proper text representation. Remember to annotate the get method as @Transient if it is an Entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.