Microsoft KB Archive/894560

= FIX: An invalid time value is inserted when you use the java.util.Calendar class and the java.sql.Date class through the SQL Server 2000 Driver for JDBC =

Article ID: 894560

Article Last Modified on 10/7/2005

-

APPLIES TO


 * Microsoft SQL Server 2000 Driver for JDBC

-





SYMPTOMS
You will experience the following problems when you use the java.util.Calendar and the java.sql.Date class with the Microsoft SQL Server 2000 Driver for JDBC:
 * When you insert a fully-qualified afternoon datetime value into a table in SQL Server, the insert will yield a time value of 12:00:00.000.
 * When you insert a fully-qualified morning datetime value into a table in SQL Server, the insert will yield a time value of 00:00:00.000.
 * When you use the java.text.SimpleDateFormat to query the the datetime data that is inserted from Java, only the morning datetime matches because the time value is 00:00:00.000.

Note The java.text.SimpleDateFormat is &quot;yyyy-mm-dd.&quot;



RESOLUTION
A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel. Date        Time    Version     Size     File name - 1-Oct-2004  12:04   2.2.0043   286,953   Msbase.jar 1-Oct-2004  12:04   2.2.0043    67,167   Mssqlserver.jar 1-Oct-2004  12:04   2.2.0043    59,072   Msutil.jar



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the behavior
  Create a sample table by using the following script: USE pubs GO CREATE TABLE MillisecTest(id INT IDENTITY(1,1), TimeTest DATETIME) GO   Compile and run the following Java code. import java.sql.*; import java.io.*;

public class Test {   public static void main(String[] args) {       try {           Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot; ); Connection connection = DriverManager.getConnection(&quot;jdbc:microsoft:sqlserver://:1433;databasename=pubs;SelectMethod=direct&quot;, &quot;&quot;, &quot;&quot;); PreparedStatement stmt = connection.prepareStatement(&quot;INSERT INTO MillisecTest(TimeTest) VALUES(?)&quot;);

// first date - afternoon java.util.Calendar ca = java.util.Calendar.getInstance; ca.set(java.util.Calendar.YEAR,2004); ca.set(java.util.Calendar.MONTH,0); ca.set(java.util.Calendar.DAY_OF_MONTH,31); ca.set(java.util.Calendar.HOUR_OF_DAY,17); //ca.set(java.util.Calendar.HOUR,5); //ca.set(java.util.Calendar.AM_PM,1); ca.set(java.util.Calendar.MINUTE,31); ca.set(java.util.Calendar.SECOND,10); ca.set(java.util.Calendar.MILLISECOND,465); System.out.println(&quot;java.util.Calendar information: \n&quot; + ca.toString);

// Workaround -> clear the time information explicitly //ca.clear(java.util.Calendar.MINUTE); ca.clear(java.util.Calendar.SECOND); ca.clear(java.util.Calendar.MILLISECOND); ca.set(java.util.Calendar.HOUR_OF_DAY,0);

// examine the values java.sql.Date mydate = new Date(ca.getTimeInMillis); System.out.println(&quot;\njava.sql.Date: &quot; + mydate.toString); java.util.Date udate = new java.util.Date(ca.getTimeInMillis); System.out.println(&quot;java.util.Date: &quot; + udate.toString);

// insert into the database stmt.setDate(1, mydate); stmt.executeUpdate;

// second date - morning ca.set(ca.HOUR_OF_DAY,10); ca.set(ca.MINUTE,24); ca.set(ca.SECOND,15); ca.set(ca.MILLISECOND,888); System.out.println(&quot;\n\njava.util.Calendar information: \n&quot; + ca.toString);

// examine the values mydate = new Date(ca.getTimeInMillis); System.out.println(&quot;\njava.sql.Date: &quot; + mydate.toString); udate = new java.util.Date(ca.getTimeInMillis); System.out.println(&quot;java.util.Date: &quot; + udate.toString);

// insert into the database stmt.setDate(1, mydate); stmt.executeUpdate;

System.out.println(&quot;\nSuccess!\n&quot;); stmt.close;

// The data is in the table, so retrieve the data. stmt = connection.prepareStatement(&quot;select TimeTest from MillisecTest where TimeTest=?&quot;);

// user enters a date java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(&quot;yyyy-mm-dd&quot;); mydate = new Date(df.parse(&quot;2004-01-31&quot;).getTime); stmt.setDate(1, mydate);

ResultSet rs = null; rs = stmt.executeQuery; System.out.println(&quot;Query results:&quot;); java.sql.Date mydate1;

while (rs.next) {               mydate1 = rs.getDate(&quot;TimeTest&quot;); System.out.println(mydate1); }

connection.close; System.out.println(&quot;Done.&quot;); }       catch(Exception e)        { e.printStackTrace; }   } }  Note To use the code sample, replace ,  , and   with the name of your computer that is running SQL Server, your user id, and your password. 

