• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validating Date field and saving into DB?

 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java in General (intermediate)
 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic