[Urgent] validating Date field and saving into DB?

V

viki.sanjeeva

Hi,

There is a date field in JSP form where user will enter date in
dd-mm-yyyy format. Now before saving into DB2, I want to validate the
date format against dd-mm-yyyy format and then save into DB2.

I've tried reading SimpeDateFormat and other date format classes, but
couldn't understand. It will be great if somebody tell the following
with code.

1. How to validate Date so that it should satisfy dd-mm-yyyy format?
2. How to insert validated Date in DB2? Where ps.setDate() comes into
picture?

Note: The date column in DB2 is of type "DATE".

Bye,
Viki.
 
G

ganesh

public class Validator {
private static final String NEW_LINE = "<BR>";
private static final String DATE_FORMAT = "yyy-MMM-dd";
SimpleDateFormat SDF = new SimpleDateFormat(DATE_FORMAT);

private List errorList = new ArrayList();

// Defualt Constructor
public Validator () {
errorList = new ArrayList();
}


/****************************************************************************
*Description of getErrors Method:Returns the list.of error Messages
*

*

****************************************************************************/
public List getErrors() {
return errorList;
}



/****************************************************************************
*Description of printErrors Method:this method calls getErrors method
which
* gives List of Errors then they are
printed
* on Server Window
*

******************************************************************************/

public void printErrors() {
List l = getErrors();
if (l.size() == 0) {
log.debug("There are no errors!!");
} else {
Iterator i = l.iterator();
while (i.hasNext()) {
log.debug(i.next());
}
}
}


/****************************************************************************
*Description of validate method:This method is used for validating
String
* values which are Mandetory
* Typically the developer should get
the value
* from the container and then call
this method
* to check if it present of not
(i.e.it is null
* or blank.)if it finds value null
then it calls
* formatAndAdd(String message) method

*
*@Param:
- * value - Value in String format
* Message - If the value is not present then this message is added
to the
* errorList.
*

****************************************************************************/
public void validate (String value, String message) {

if (value == null || value.equals("") ) {
formatAndAdd(message);
}

}



/****************************************************************************
*Description of validateDate method:This method is used for
validating Date
* which is in String Format
* Typically the developer should
check get
* the value from the containerand
then call
* this method to check if the date
is in
* correct format or not.
*
*@Param:
* value - Date as a String Object
* Message - If the Date is not in correct format then this message
is added
* to the errorList.
*

*****************************************************************************/
public void validateDate (String value, String message) {

try {
Date d = SDF.parse(value);
System.out.println("This is the Date: " + d);
} catch (Exception e) {
formatAndAdd(message);
}

}

/******************************************************************************
*Description of formatAndAdd method:this method is called by other
methods of
* the same class for formating the
message
* and adding it errorList when
method finds
* any Validation error in input
Parameter
*
*@Param
* message:message to be added to error List
*

******************************************************************************/
private void formatAndAdd(String message) {
StringBuffer sb = new StringBuffer(NEW_LINE);
sb.append(message);
sb.append(NEW_LINE);
if (!errorList.contains(message)) {
errorList.add(sb.toString());
}
}

}

hope this will help
 
C

castillo.bryan

For validating the date, use SimpleDateFormat's parse method.
You should also set lenient to false. An exception will be thrown if
it doesn't parse.

try {
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
// this is very important, otherwise 31-09-2005 would parse as
01-10-2005
sdf.setLenient(false);
java.util.Date dt = sdf.parse("01-01-2006");
}
catch (ParseException pe) {
// invalid date format
}

To insert into DB2, get a database connection and prepare a statement,
and like you were guessing, use PreparedStatement.setDate(int pos,
java.sql.Date date);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top