my dog learned polymorphism*
The moose likes Beginning Java and the fly likes DataTime conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DataTime conversion" Watch "DataTime conversion" New topic
Author

DataTime conversion

jagan bisoyi
Greenhorn

Joined: Mar 09, 2008
Posts: 15
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
I have not used SQLSERVER, but I think that most databases accept date in
mm/dd/yyyy format.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: DataTime conversion