This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting String to Date???

 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to convert some data I get as a string into a date so I can insert it into a database. Here are the code snippets I am using.


If I send a string 9/30/2006 I get "Sat Sep 30 00:00:00 BST 2006"
back when I call getDate. The database is giving me an error of non-numeric character found where numeric value expected...

Any ideas?
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What database are you using?
What does your SQL statement look like?
What type is the database field you are trying to change?
You give us a result if you call "getDate". What do you call "getDate" on? A result set? A Date instance?
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Database = oracle;
SQL = a simple "Insert into table (field) values ( dDate );
Field = I am taking a java string and inserting it into an oracle date field (is that what you were asking?)
getDate = getDate is in a java data object (just a bunch of getters and setters) it returns a date object. when I print it out I get "Sat Sep 30 00:00:00 BST 2006" where the String was 9/30/2006.

I did develop an answer but would still like further input as I might not have the best answer.
My answer was to ignore the java Date altogether and just call the oracle to_date function in the sql I passed. The to_date function will except a string (9/30/2006) and convert it to a date. perhaps not the best solution as the database is having to do the work but it seems to be an answer.

Thanks for any input.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you will want to do is use a PreparedStatement here.



This will allow the JDBC driver to handle the date conversions. If you are using the date as a String, then you will have to call Oracles TO_DATE function.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic