• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataTime conversion

 
jagan bisoyi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
string.
Hi guys!
I am using SQLSERVER 2005+jdk1.6
Where i am sending date as a String and typcasted to date format by Simpledateformat .while inserting in database its giving error" //error is Conversion failed when converting datetime from character "
Here the code::
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
dt = formatter.parse(login_form.getDate());
// date = login_form.getDate();

System.out.println("date==============" + dt);
///////////////////////////////////////////////////
SQL statement]
stm.executeUpdate("insert into Marketing_claims
(Claim_type_id,Claim_amount,Claim_comments,Claim_date,Employee_id)
values(" + str + " ," + amount + ",'" + claimComment + "','" + dt +
"'," + empid + ")");
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have not used SQLSERVER, but I think that most databases accept date in
mm/dd/yyyy format.

Hope this helps
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This what API say , about SimpleDateFormat parse method ..

Date parse(String text, ParsePosition pos)
Parses text from a string to produce a Date.


so , you need to pass 'pos' variable , that solves the error ..

regards,
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Ghorpade:
Hi,
I have not used SQLSERVER, but I think that most databases accept date in
mm/dd/yyyy format.

Hope this helps

I think it depends on the regional settings, but I always just use yyyy-MM-dd to make sure. That format never gets interpreted incorrectly.

Another good idea is of course to use PreparedStatement, and use the setDate or setTimestamp methods. You'll need to convert a java.util.Date object to a java.sql.Date or java.sql.Timestamp object for that, but that's easy:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic