Formatting the date values in a form page

In JSF we have 3 text boxes to capture dayValue,monthValue and yearvalue where the user have option to enter present and future date (MAX 1 year in future).

I am capturing those values with properties dayValue,monthValue and yearvalue in controller and convert to string to a particular format (see below)

SimpleDateFormat date = new SimpleDateFormat();


String userEnteredDate = monthValue + "/" + dayValue + "/" + yearValue;

and iam sending this String as date instance (effectiveDate see below)to the back end (DB)

try {

userDate = date.parse(userEnteredDate);


But the problem here is if the user entered date in either any of the options in text boxes.
1. feb 30

2. april 31

3. sep 30

4. nov 30

while sending it as date object to DB its sending as

1. feb 03

2. may date or april 1st

etc etc.

Can i have ur help to resolve this issue so that it should send user entered entry or any common validations are there to resolve this issue (we dont have calender icon to be displayed in JSF )
Who is Participating?
guru_samiConnect With a Mentor Commented:
Not a JSF guy but one way would be to make a check before you concate all values to userEnteredDate.
if(mothnvalue==2(i.e. feb))
{ //logic to check if yearValue is leapyear or not
  //then check if dayValue > 28 or dayValue >29 depeding
else if( monthvalue=1|3|5|7|8|10|12)
{ if dayValue >31....not valid
{ if dayValue >30....not valid

Now even if you use DropDownlist you will need such kind of check.
But above pseudo code should work no matter if you use DDL or Textboxes.
Cant you use simple dropdowns with fixed values for month and year.
That way it saves lots of validation.Just a thought
Use a regular expression to validate the input in javascript.Here is the one I used


if you can use change to just using 1 text box for the date then you can use the built in validator to validate patterns and values.
<t:inputText maxlength="10" forceId="true" id="mydate" value="#{myobject.mydate}" required="true">
        <f:convertDateTime type="date" pattern="MM/dd/yyyy" />

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.