• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Converting String to Date???

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic