1

I am using sqljdbc4.jar to connect SQL Server from my java application. I have a date field in my table. It works normally when I check from Sql Server Management Studio. But in java, every date is 2 day missing.

For example;

my date : 2012-01-10

date in java : 2012-01-08

PreparedStatement stmt = conn.prepareStatement("SELECT * FROM table1", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery();
rs.next();
System.out.println(rs2.getDate("dateCol").toString());

Why?

5
  • 1
    Could you try using SimpleDateFormat with yyyy.MM.dd format instead of .toString method? Commented May 8, 2012 at 8:47
  • Check your locale settins for both your database and java, are you sure that you are using the same time zone for both? Commented May 8, 2012 at 8:47
  • 2
    Same error here; proposes to use an other driver, JTDS. Commented May 8, 2012 at 8:47
  • This question is a duplicate of the one that Joop Eggen linked to. Commented May 8, 2012 at 8:59
  • I just tried sqljdbc4.jar version 4, it fixed. Also there is no problem with version 2. Only bugged version is 3. Download link for version 4 Thanks for answers. Commented May 8, 2012 at 9:00

4 Answers 4

1

I just tried sqljdbc4.jar version 4, it fixed. Also there is no problem with version 2. Only bugged version is 3. Download link for version 4.

Thanks for answers.

Sign up to request clarification or add additional context in comments.

1 Comment

If using Tomcat, don't forget to also check the lib directory for older versions of this jar. It will override whatever is in your webapp.
0

which version of SQL Server and which datatype are you using? There are 6 "date and time" data types on SQl Server 2008:

date
datetimeoffset
datetime2
smalldatetime
datetime
time

maybe the one you are using is not supported in Java or by the drive you are using. Specially if you are using Date which is new in SQL Server 2008

Comments

0

like @Eugenio Cuevas said please try formatting the date

Could you try using SimpleDateFormat with yyyy.MM.dd format instead of .toString method?

because looking at you date example ; may be its like a problem i faced before the difference is 2 day because

1- 2012-01-10 and 2012-01-08 yyyy-mm-dd 2- 12-01-10 and 12-01-08 yy-mm-dd

that what caused my problem

Comments

0

I was faced with somewhat similar problem couple of weeks. I had to save a java date in an oracle table which is typed sql date. You can not directly cast a java date to sql date. The following is what I did.

java.util.Date jDate = myObj.getDate();
java.sql.Date sDate = new java.sql.Date(jDate.getTime());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.