What is the cleanest way to display a java.util.Calendar object in JSF? - jsf

We have an object with java.util.Calendar objects. We would like to display the data on a JSF page (preferably in the same format we have for java.util.Date objects). It seems like there should be some clean way to do this other than creating a wrapper class just to convert the Calendar to a Date.
What is the cleanest way to display the date/time held in a java.util.Calendar in a JSF page?

Use Calendar's own getter Calendar#getTime(). It returns a Date. Then you can use <f:convertDateTime> the usual way.
<h:outputText value="#{bean.calendar.time}">
<f:convertDateTime type="both" dateStyle="long" />
</h:outputText>

For others and #seangates: You CAN apply a pattern if using the Calendar object. E.g.
<h:outputText value="#{bean.calendar.time}" styleClass="date">
<f:convertDateTime pattern="EEEE, MMMM d yyyy" />
</h:outputText>
<h:outputText value="#{bean.calendar.time}" styleClass="time">
<f:convertDateTime pattern="hh:mm" />
</h:outputText>

If you need futher Text:
<h:outputFormat
value="#This my date: {0,date,long}">
<f:param value="#{bean.calendar.time}" />
</h:outputFormat>
see: https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html

Related

Primefaces datatable Identify if the current column is of date type

I am trying to make a common datatable using primefaces.
I want to check if the current column of datatable is of Date type. If it is a Date I want to add DatetimeConverter -
Here is my code -
<ui:component>
<p:dataTable id="#{tableId}" value="#{data}" var="row" styleClass="stdTable vertLines fndTable vertLinesRightBorder"
style="table-layout:fixed; border-color: #dddddd;">
<p:columns value="#{tableColumns}" var="column" sortBy="#{row[column.property]}">
<f:facet name="header">
#{column.header}
</f:facet>
#{row[column.property]}
</p:columns>
</p:dataTable>
</ui:component>
I am calling the above xhtml as follows-
<ui:include src="table.xhtml">
<mbcpos:param name="tableId" value="#{me.id}hTabel1" />
<mbcpos:param name="data" value="#{taskListBean.receivedOwnerTasks}" />
<mbcpos:param name="tableColumns"
value="#{me.columns}" />
</ui:include>
Can we identify the column datatype :
e.g., #{row[column.property]} equals Date
If the column is a date I want to format the date.
Your column model should be where you put the data type it holds and other data type presentation related things like masks, currency, alignment etc.
On the page, for each data type (according to your needs), inside the p:columns
you should do something like this:
<h:outputText rendered="#{column.dateType}" value="#{row[column.property]}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>

MM/dd/yyyy format in p:calendar

In reference to this question,
it still accepts 8/1/201987 as a date, where in 201987 is not in yyyy format.
How to ensure yyyy format? Do I need to validate it in the bean?
I have used the code below :
<p:calendar ...>
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
But as it uses SimpleDateFormat, if you give yyyy it accepts any number of digits.
How to resolve this issue?
Try mask for p:calender like mask="99/99/9999", it's works fine for me
<p:calendar value="8/1/201989" mask="99/99/9999">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
You can restrict the user input by adding attributes mask="true" and maxlength=10

How to use today's date for a placeholder?

I got a <p:calendar> and want to use today's date as placeholder.
My attempt is to use OmniFaces #{now} in combination with a:placeholder for this:
<p:calendar id="calendar" a:placeholder="#{now}" minHour="8" locale="de"
maxHour="20" value="#{adminSeminarAlleBearbeitung.seminar.someproperty}"
pattern="dd.MM.yyyy HH:mm" />
Where someproperty is a java.util.date.
This is working so far, but I want to format the date like dd.MM.yyyy HH:mm.
How can I achieve this?
Use of:formatDate() function in EL:
<p:calendar ... a:placeholder="#{of:formatDate(now, 'dd.MM.yyyy HH:mm')}" />
Or if you'd like to reuse calendar component's own pattern attribute:
<p:calendar ... a:placeholder="#{of:formatDate(now, component.pattern)}" pattern="dd.MM.yyyy HH:mm" />
See also:
What exactly is #{component} in EL?

regex date validation on p:calendar

my calendar has readOnlyInput="false" , hence the user can enter wrong dates such as 13/13/2013.
is there a way to regexValidate my date in case the user prefers to type the date instead of using the datePicker popup ?
<p:calendar id="birthDate" size="22" locale="#{view.locale}"
maxdate="#{userCreationBean.maxDate}" navigator="true"
yearRange="c-100" readOnlyInput="false"
value="#{userCreationBean.user.birthDate}"
mindate="01/01/1900" pattern="dd/MM/yyyy"
style="left: 194px !important;"
>
</p:calendar>
The <f:validateRegex> validator works on String input values only, not on Date input values and is therefore insuitable for the purpose you had in mind.
Rather use the <f:convertDateTime> converter.
<p:calendar ...>
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
It's by default non-lenient and will thus throw a converter exception when an invalid date is entered. You can if necessary customize the converter message by converterMessage attribute on the input component.

How to reuse the jsf component parameters?

I have code
<p:column id="columnLastComment" headerText="LAST COMM" width="80"
styleClass="long-field" filterStyleClass="flt-hidden"
style="#{alarmTable.getDisplayStatus('columnLastComment')}"
sortBy="#{alarm.lastComment}" filterBy="#{alarm.lastComment}">
<h:outputText value="#{alarm.lastComment}" />
<f:facet name="header">
<h:outputText value="LAST COMM" title="Last Comment"
styleClass="tableHeader-fontSize" />
</f:facet>
</p:column>
How i can use id of column in style value? example -
style="#{alarmTable.getDisplayStatus('[id of this column]')}"
Theoretically, you could get away with the implicit EL variable #{component} to get the current component, like so:
<h:outputText id="foo" styleClass="#{bean.styleClass(component.id)}" />
But, unfortunately, the UIColumn component has a special treatment. The #{component} refers the parent UIData component instead, which would in your case be the <p:dataTable>.
So, you've really to repeat it (like as you're already doing for the phrase "lastComment". You could maybe make it less painful by using a property of #{alarm}.

Resources