Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date validation in java

 
Amrita Datta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two date pickers. I need to implement a validation that my "from date" is equal to or greater than the current date(or today's date).
Am using Springs. I have taken care of all the other validations in the validator class. Just need to implement this one.
I have used SimpleDateFormat for validating that my from date should be less or equal to my to date.

Below is the code snippet..
------------------------------------------------------------------------
try
{
boolean toValid = true;
boolean fromValid = true;

String toDate=crf.getTo();
String fromDate=crf.getFrom();

if((!(toDate.trim().length() > 0)))
{
toValid=false;
errors.rejectValue("to", "errordate.not-specified",null,"Date required");
}

if ((!(fromDate.trim().length() > 0)))
{
logger.error("fromDate is Empty");
fromValid=false;
errors.rejectValue("from", "errordate.not-specified", null, "Date required.");
}
if(toValid&&fromValid){
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
logger.info("INSIDE VALIDATION");
Date toDateParsed = sdf.parse(toDate);
Date fromDateParsed = sdf.parse(fromDate);
logger.info(toDateParsed.getDay());
if(toDateParsed.before(fromDateParsed)){
errors.rejectValue("from", "error.incorrectRange", null, "To Date must be greater than From date");
}

}

-------------------------------------------------------------------------------

How do i compare and check if my from date is equal to or greater than the current date?
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the API documentation for the Date class. You will see a method whose description says "Tests if this date is before the specified date." Use that method.

And if you didn't already know: new Date() gets you a Date object containing "now".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic