my dog learned polymorphism
The moose likes Java in General and the fly likes date validation in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "date validation in java" Watch "date validation in java" New topic

date validation in java

Amrita Datta

Joined: Mar 13, 2009
Posts: 3
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..
boolean toValid = true;
boolean fromValid = true;

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

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

if ((!(fromDate.trim().length() > 0)))
logger.error("fromDate is Empty");
errors.rejectValue("from", "errordate.not-specified", null, "Date required.");
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");"INSIDE VALIDATION");
Date toDateParsed = sdf.parse(toDate);
Date fromDateParsed = sdf.parse(fromDate);;
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

Joined: Oct 14, 2005
Posts: 19738

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".
I agree. Here's the link:
subject: date validation in java
It's not a secret anymore!