Java JDBC: dates consistently two days off
For those who are using maven, use this for java 8:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.1.jre8</version>
</dependency>
As it is pointed out, if you use an old version, you may face this problem, and it is not easy to debug.
like what user903724 said, change to version 4 that would be fixed, but in my case, When I'm using sqljdbc4-3.0.jar, this issue still reproduces, but I change it to sqljdbc42.jar, this issue got fixed. Hope my experience would be helpful. download from sqljdbc42.jar
Faulty JDBC Driver
It turns out the problem was the MS JDBC driver. I tried every possible combination of date types and date conversions and nothing worked. After a great deal of searching (should have done that first!) I saw a comment on an older SO entry that implied the problem was the version 3 JDBC driver from Microsoft. I got the latest driver, version 4.something, and the problem went away.
Thanks to all that tried to help. Special thanks to you Mike for taking the time to post a solution.
-=beeky
I've run into this problem a couple of times recently and tore my hair out before remembering that if the field is of type date this happens, switching the field type to type datetime solves the problem.