File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes validating Date field and saving into DB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "validating Date field and saving into DB?" Watch "validating Date field and saving into DB?" New topic
Author

validating Date field and saving into DB?

Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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.
[ December 20, 2005: Message edited by: Vikrama Sanjeeva ]

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Annie Smith
Ranch Hand

Joined: Mar 05, 2005
Posts: 172
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?

Read the JDBC tutorial for PreparedStatement.


Cheers!<br /><b>Annie</b>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Moved to Java in General (intermediate)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
Hi,

1. How to validate Date so that it should satisfy dd-mm-yyyy format?

This is working fine with same code mentioned by Annie. It is validating the required format.

2. How to insert validated Date in DB2? Where ps.setDate() comes into picture?

Here I'm having problems. It goes like this:

After validating the date enter by the user in dd-mm-yyyy format, I convert the string date (validatedDate) into java.sql.Date, let' say it sqlDate. Now, when I'm printing sqlDate it is showing entirely different date.


Here is the code:



I cannot guess what is going wrong here in conversion?

Bye,
Viki.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The API for java.sql.Date reads:

Converts a string in JDBC date escape format to a Date value.

Parameters:
s - a String object representing a date in in the format "yyyy-mm-dd"

Is your Sstring object representing a date in in the format "yyyy-mm-dd"?


There is no emoticon for what I am feeling!
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
IN the above code if we include one more line:
df.format(dateString)
the datefield from the form is formatted to the required format specified.. then continue coding for storing it in db.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Balaji Sampath:
IN the above code if we include one more line:
df.format(dateString)
the datefield from the form is formatted to the required format specified.. then continue coding for storing it in db.

Doesn't format take a Date object, not a String?

Consider doing the following to go from "dd-MM-yyyy" to java.util.Date.

Are you out of the woods? I'm not sure, because you may have to set the
time zone of your DateFormat's time zone to, say, UTC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validating Date field and saving into DB?
 
Similar Threads
conversion of date
Code review required for converting dateformat
datefiled in dd-mm-yyyy format
dates
Date Validation