Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date validation

 
Shein Zeya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying a Struts Project in which validation of date is included.There..I have to validate the input date whether it is valid or not.If the date is not in the past but in the future..that's invalid.

In the process of trying I found some difficulties.
In my program....whatever user entered..just appear the invalid Date error message.Here's my codes




String userDob=aForm.getDob();

//here Date currentDate = new Date();
java.util.Date currentDate = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");


Calendar udob= new GregorianCalendar();
Calendar cdate = new GregorianCalendar();

try { udob.setTime((Date) formatter.parse(userDob));
cdate.setTime(currentDate);
}
catch (ParseException e)
{
DefaultLogger.debug("AValidator",(e.getMessage()));
}
if(udob.compareTo(cdate)>0)
//DefaultLogger.debug("AValidator","DOB is Future Date"+ udob);
DefaultLogger.debug("this", "DOB is Future Date" + aForm.getDob());
errors.add("dob", new ActionError("error.string.dobinvalid", "1", "20"));
}





return errors;
}



Please do help me....Thank you
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you're doing is comparing one date to another to see which is later, you don't need to use a java.util.Calendar. Just use a java.util.Date. Example:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic