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

validating Date field and saving into DB?

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (intermediate)
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic