wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DataTime conversion" Watch "DataTime conversion" New topic

DataTime conversion

jagan bisoyi

Joined: Mar 09, 2008
Posts: 15
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
values(" + str + " ," + amount + ",'" + claimComment + "','" + dt +
"'," + empid + ")");
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2716

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

Hope this helps

|Asking Good Questions|
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902

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 ..


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

Joined: Oct 27, 2005
Posts: 19656

Originally posted by Amit Ghorpade:
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:

How To Ask Questions How To Answer Questions
wood burning stoves
subject: DataTime conversion
Similar Threads
How to print default date in required format
Date Formatting
Problems with custom xsd:dateTime conversion
How does a parse method in Date class work
Convert Calendar object to Date object