Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Date output in JSF

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?

like image 397
Albert Gan Avatar asked Jan 19 '11 12:01

Albert Gan


People also ask

How to convert string to date in JSF?

SimpleDateFormat df = new SimpleDateFormat(" dd MMM yyyy"); Date oneDate = new Date(startDate); df. format(oneDate);

How to input date in JSF?

JSF: Handling Dates in JSF Forms The user enters the student data into the form: first name, last name and hire date. The input format for the hire date is day, month, year. As an example For 1 May 2016, you would enter: 1-5-2016. Once the form is submitted, then the app will show the output.


2 Answers

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> 
like image 68
jmj Avatar answered Nov 04 '22 14:11

jmj


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.

like image 31
Martin Höller Avatar answered Nov 04 '22 14:11

Martin Höller