JSF Date Output Format

If #{myBean.birthdate} is of type java.util.Calendar or java.util.Date , can I format it inside the EL itself using an existing function, possibly with an output similar to the one created using DateFormat SHORT , MEDIUM , LONG abd FULL output type?

Instead of displaying the full form for #{myBean.birthdate} : Wed Jan 19, 19:01:42 WIT 2011 , I just prefer the simple conclusion on January 19, 2011 .

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

+47
el jsf date-formatting
Jan 19 '11 at 12:11
source share
3 answers

Use <f:convertDateTime> . You can embed it in any input and output component. Template rules are the same as java.text.SimpleDateFormat .

 <h:outputText value="#{someBean.dateField}" > <f:convertDateTime pattern="dd.MM.yyyy HH:mm" /> </h:outputText> 
+128
Jan 19 '11 at 12:15
source share

If you use OmniFaces , you can also use it for EL functions such as of:formatDate() to format Date objects. You would use it as follows:

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

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

+23
Dec 16 '13 at 13:31
source share

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 recipient to the bean, resulting in

  #{myBean.birthdateString} 

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

+6
Jul 09 '11 at 12:22
source share



All Articles