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 (894560)



The information in this article 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 "yyyy-mm-dd."

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: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 "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a sample table by using the following script:
    USE pubs
    GO
    CREATE TABLE MillisecTest(id INT IDENTITY(1,1), TimeTest DATETIME)
    GO
    
  2. 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("com.microsoft.jdbc.sqlserver.SQLServerDriver" );
    			Connection connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://<Server>:1433;databasename=pubs;SelectMethod=direct", "<UserId>", "<PassWd>");
    			PreparedStatement stmt = connection.prepareStatement("INSERT INTO MillisecTest(TimeTest) VALUES(?)");
    
    			// 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("java.util.Calendar information: \n" + 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("\njava.sql.Date: " + mydate.toString());
    			java.util.Date udate = new java.util.Date(ca.getTimeInMillis());
    			System.out.println("java.util.Date: " + 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("\n\njava.util.Calendar information: \n" + ca.toString());
    
    			// examine the values
    			mydate = new Date(ca.getTimeInMillis());
    			System.out.println("\njava.sql.Date: " + mydate.toString());
    			udate = new java.util.Date(ca.getTimeInMillis());
    			System.out.println("java.util.Date: " + udate.toString());
    
    			// insert into the database
    			stmt.setDate(1, mydate);
    			stmt.executeUpdate();
    
    			System.out.println("\nSuccess!\n");
    			stmt.close();
    
    			// The data is in the table, so retrieve the data.
    			stmt = connection.prepareStatement("select TimeTest from MillisecTest where TimeTest=?");
    
    			// user enters a date
    			java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-mm-dd");
    			mydate = new Date(df.parse("2004-01-31").getTime());
    			stmt.setDate(1, mydate);
    
    			ResultSet rs = null;
    			rs = stmt.executeQuery();
    			System.out.println("Query results:");
    			java.sql.Date mydate1;
    
    			while (rs.next())
    			{
    				mydate1 = rs.getDate("TimeTest");
    				System.out.println(mydate1);
    			}
    
    			connection.close();
    			System.out.println("Done.");
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Note To use the code sample, replace Server, UserId, and PassWd with the name of your computer that is running SQL Server, your user id, and your password.

REFERENCES

For more information about JDBC, click the following article number to view the article in the Microsoft Knowledge Base:

313100 How to get started with Microsoft JDBC

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates


Modification Type:MinorLast Reviewed:10/7/2005
Keywords:kbDateTime kbJDBC kbProgramming kbQFE kbfix KB894560 kbAudDeveloper