File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Date in Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Date in Database" Watch "Date in Database" New topic
Author

Date in Database

Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
I am having a database in firebird. There is a field DAT_OF_BIRTH in a table, its type is TIMESTAMP (which can properly maps over java.sql.Date).

But the problem is this that in this db's field accept date in format of yyyy-mm-dd.
While my user interface sends in dd-mm-yyyy. On middle tier Iv tried to use java.text.Dateformat but still its not working.

Could you please tell me a way to change representation of Date.


Regards,<br /> <br />Ahsan Jamil<br />SCJP, SCWCD, IBM OOAD
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Use a PreparedStatement and bind your data parameters as Date objects. Then you have no formatting issues.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi. I'm not familiar with firebird, so not sure what the problem is?

Did you try something like this?

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date myDate = dateFormat.parse( myDateString );
...
preparedStatement.setTimestamp( myDate );
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Donot care about the representation of the date while writing it to DB. But when it comes to presentation then offcourse you need to format it.

Use the prepared statement, as Paul said.
Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
Originally posted by Paul Sturrock:
Use a PreparedStatement and bind your data parameters as Date objects. Then you have no formatting issues.


Well, Its not adding proper data in db.

If im doing
pStmt.setDate(Date.valueOf("01-01-2003"))

In DB, its inserting 0006-06-26.

And by the way, In DB this field's type is TIMESTAMP. But im not able to use timestamp as well in java.
Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
Originally posted by Horatio Westock:
Hi. I'm not familiar with firebird, so not sure what the problem is?

Did you try something like this?

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date myDate = dateFormat.parse( myDateString );
...
preparedStatement.setTimestamp( myDate );


setTimeStamp must need a TimeStamp object as a Parameter. And TimeStamp is a subclass of java.util.Data
Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
Originally posted by Adeel Ansari:
Donot care about the representation of the date while writing it to DB. But when it comes to presentation then offcourse you need to format it.

Use the prepared statement, as Paul said.


Even its not storing data in db properly, its changing date 0021-12-03
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Re-read the JavaDocs for java.sql.Date.valueOf(String s). You are parsing a date like this:

which represents the 26th June 0006 apperently. However:

is the 1st of January 2003.
Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
Ok. But my requirement is that I have to send Date in dd-mm-yyyy format from front end and Middle tier should have a logic to convert it into yyyy-mm-dd , because DB can only contain in that format.

Thats why I have to send something like 01-01-2003
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No, you don't. Databases do not store dates as Strings.

Presuming your front end is allowing users to input dates in this format: dd-MM-yyyy, then you need to add something to parse these strings there (have a look at java.text.SimpleDateFormat). Once you've parsed it, you can pass it about as a Date object. 10-01-2003, 2003-01-01, 1st Jan 2003, 01-Jan-2003, 1/1/2003 etc. are all different presentations of the Date value 1041379200000. When you bind this Object to a PreparedStatement it is the value not the format which is bound.
Ahsan Jamil
Ranch Hand

Joined: Aug 24, 2001
Posts: 58
Thanx for your help, It has been sorted out now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date in Database