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?
Related
I am using the component to allow the user pick the date they were born (Birth Date). I should display an error message if the user picks a day greater than today's date to prevent the age for been a negative number. My component looks like this:
<p:calendar id="fechaNacimiento" yearRange="c-100:c"
pattern="dd/MM/yyyy" navigator="true"
value="#{afiliadoController.afiliado.fecha_nacimiento}"
requiredMessage="Debe de insertar la fecha de nacimiento del Afiliado."
showOn="button"
readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
required="#{request.getParameter('validate')}">
</p:calendar>
I am using PrimeFaces and OmniFaces and JSF 2.2. I was trying to validate this by using <o:validateOrder components=""/> but I would need two component and I only have one. What other options do I have?
PrimeFaces <p:calendar> has a maxdate attribute representing maximum selectable Date.
OmniFaces has a #{now} variable representing current Date.
Do the math.
<p:calendar ... maxdate="#{now}" />
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
I have a <p:calendar> code in XHTML
<p:calendar
value="#{Bean.targetdate}"
pattern="MM/dd/yyyy" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
My problem is when I provide a invalid date such as 12/31/6666 the <p:calendar> accept the date as it is and I did SOP in the backing bean and I got Mon Dec 31 00:00:00 IST 6666 for the date that I enter.
I should not allow the user to enter a invalid date like the one that I entered above.
And I need to make sure the user should provide a valid date How can I do that ?
You can use yearRange property to reslve your problem, like-
<p:calendar yearRange="c-125:c+50" value="#{Bean.targetdate}" pattern="MM/dd/yyyy" readonlyInput="true" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);"/>
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.
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