Format Date output in JSF
Asked Answered
S

3

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?

Sacci answered 19/1, 2011 at 12:11 Comment(0)
S
174

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>
Skipper answered 19/1, 2011 at 12:15 Comment(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}" ... />.Sacci
@bertie: see my answer below and use OmniFaces.Valuable
how is it possible to check the year or month for example ?Paramaribo
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?Curiosa
@Curiosa - 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.Skipper
Thanks. My question is not a quick-hack, get it work somehow (I already got that), but about how are we supposed to use this. JSF can convert user text input containing a date and no time to a java.util.Date object using the f:convertDateTime. Is this a good idea at all, if the Date represents a point in time and if the conversion takes place at the server, it will produce a wrong point in time, i.e. another one than the user at the client intended? Anyway, any idea how to tell the h:inputText to produce a date picker in the browser?Curiosa
A
29

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.

Aliciaalick answered 16/12, 2013 at 13:31 Comment(1)
With OmniFaces approach it's more intuitive than the f:convertDateTime.Tableau
P
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.

Psychographer answered 9/7, 2011 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.