• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting Date for JDBC  RSS feed

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi all,

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

Thanks again,
Jehan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!