This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "date validation in java" Watch "date validation in java" New topic
Author

date validation in java

Amrita Datta
Greenhorn

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..
------------------------------------------------------------------------
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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: http://aspose.com/file-tools
 
subject: date validation in java
 
Similar Threads
date validations
Validation for the Indexed properties dynamic fields in struts (JSP)
Compare two custom date input fields each having 3 inputText fields using validator
how to convert a string to date
Display cookie value in GSP (Groovy Server Pages) :