• 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

Date Conversion

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question that I'm hoping there is an easy answer to..... I don't want the solution to be too messy, ie string manipulation if i can help it.

I am querying the user to enter a date, in the format dd-mmm-yyyy, in other words 12-Dec-2004(This is a UK application), however in order for me to convert the date to a timestamp for use with SQL Server, it must be in the format yyyy-mm-dd. Is there an existing class that handles conversions of this sort or am i looking at writing a method to do it?
Thanks alot
 
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
java.text.SimpleDateFormat
Please avoid the common mistake and use 2 different instances. One to parse the incoming data and one to format the outgoing data.
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It strikes me that you may be going the wrong way about this.
If you're using JDBC to run a query on the database, it would be safer to use a PreparedStatement, and then use the setTimestamp() method to set parameters in your SQL. This takes care of all the necessary conversions behind the scenes.
Generating SQL strings directly should generally be avoided, if possible, because if you're not careful it can leave you open to SQL insertion attacks.
David Peterson
reply
    Bookmark Topic Watch Topic
  • New Topic