• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Date in Database

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a PreparedStatement and bind your data parameters as Date objects. Then you have no formatting issues.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for your help, It has been sorted out now
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic