• 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

Formatting Date for JDBC

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using Spring JDBC template to insert a value in a table, among the values is a Date field..



My dilemma is how do I format the date correctly before passing it in. Right now the date value is coming out in Java as "'Mon Mar 04 14:14:59 EST 2013" and my DB does not seem to like it. The SQL data type is date but it will only take values like "03/04/2013". How can I get this value in Java without changing the date into a String. Because if I do it complains of data type mismatch.

Thanks in advance for any help.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't format the date. You use a java.sql.Date object (or Timestamp, if you want to include both date and time) and an appropriate DATE or DATETIME or whatever SQL column, and then the appropriate PreparedStatement.setXxx() method.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the Date object has to be a java.sql.Date object, not a java.util.Date object.

Unfortunately, there are two different classes called "Date" in Java's standard API. (And even worse, java.sql.Date incorrectly extends java.util.Date, and there are even more things wrong with it, such as that they are not immutable).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jehan Jaleel wrote:Hi,


Jehan,

Please DontWriteLongLines (←click). It makes your thread very hard to read.
I'd break them up myself but there's quite a lot to do.
Just use the 'Edit' button to put it right; and always check your posts with the 'Preview' button before you hit 'Submit'.

Thanks

Winston
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for your help, changing from util to sql Date resolved the issue.

Thanks again,
Jehan
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic