Cannot convert input to timestamp in JSF - jsf

I'm trying to save a Date and Time thats entered by a user as a Timestamp in JSF but I keep getting this error: "Cannot convert 23.02.19 12:00 of type class java.util.Date to class java.sql.Timestamp"
This is my code thats supposed to converrt the input "23.02.2019 12:00" to a Timestamp.
<h:inputText value="#{eventMB.eventTo.datum}">
<f:convertDateTime pattern="dd.MM.yyyy hh:mm" />
</h:inputText>

Related

Validate p:calendar value to be not greater than today's date

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}" />

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?

JSF - Formattting <p:calendar> using SimpleDateFormat

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);"/>

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.

Resources