pls,i need help in inserting a date from a text field using java, to mssql database.
i am getting incorrect date for example when i insert 13/3/2010,i am getting the date
1/3/2011.i want a solution,i've searched a lot but useless.
the code is:

try{
                java.util.Date d; 
                String s;
                d = new java.util.Date(t.getText().trim());
                SimpleDateFormat df =new SimpleDateFormat("MM/dd/yyyy");
                s=df.format(d);
                System.out.println(s);
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection conn = DriverManager.getConnection               ("jdbc:sqlserver://localhost:1433;DatabaseName=employee;user=sa;password=moujtaba"); 
                Statement insert=conn.createStatement();
                insert.executeUpdate("insert into constructionmaterial(inward,date,reference) values (15,'" + s + "',1)");
               
               }catch(ClassNotFoundException cnfe) 
                { 
                 System.err.println(cnfe); 
                }
               catch (SQLException sqle) 
               { 
                System.err.println(sqle); 
               }  
         }

According to your date formatter, you are expecting month to come first. 13 is not a valid month.

i apreciate ur respone
even when i put dd\MM\YYYY
i am getting the same thing.it is inserting in my mssql a wrong date.
i've tried the 2 cases
thanx

You want the date March 13, 2010, right? Try leaving your date formatting as "MM/dd/yyyy" and input the date as "03/13/2010".

Edited 6 Years Ago by kramerd: n/a

yes kramerd
it worked fine, it inserted it as u give it to me in the database i.e 03/13/2010.
but what if i want the day then month i mean 13/3/2010.
what must i do,bcs when i put it dd\MM\yyyy it is not accepting 13/3/2010 and it is inserting it 1/3/2011
thanx

OK, you are printing the date from your java code. So if your date formatter is expecting dd/MM/yyyy and you enter 13/03/2010, is your output statement printing 13/03/2010? If so, this is a MSSQL problem and not a Java problem. If not, then what is being printed?

yes,
so what do u think about mssql to format it in the way i want to present day then month then year.
thanx

Sorry, I don't know much about MSSQL. Maybe you need to find a different forum that deals with database issues.

This article has been dead for over six months. Start a new discussion instead.