Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4107391

java.sql.Date(long) instances differ when created in different time-zones.

XMLWordPrintable

    • 1.2beta4
    • sparc
    • solaris_2.5
    • Verified



      Name: avC70361 Date: 01/28/98



        java.sql.Date instances created with constructor java.sql.Date(long millis) in
      different time-zones with the same millis value are not equal. That is caused by
      the fact that the constructor wipes time fields and then sets year, mounth and day
      fields with values extracted from millis parameter using default TimeZone.
      Therefore, when created in time-zones with different offsets, two java.sql.Date
      instances are representing different points in time.

      Here is the test demonstrating the bug:
      --------------SQLDateTest.java-------------
      import java.sql.Date;
      import java.util.TimeZone;

      public class SQLDateTest {

        public static void main(String args[]) {
          long date = 123456789L;

          TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
          Date date1 = new Date(date);
          System.out.println(new java.util.Date(date));

          TimeZone.setDefault(TimeZone.getTimeZone("PST"));
          Date date2 = new Date(date);
          System.out.println(new java.util.Date(date));

          if (date1.equals(date2)) {
            System.out.println("Passed.");
          } else {
            System.out.println("Failed." + date1 + " != " + date2);
          }
        }

      }
      -----------------The test output-------------
      > java SQLDateTest
      Fri Jan 02 10:17:36 GMT+00:00 1970
      Fri Jan 02 02:17:36 PST 1970
      Failed.1970-01-02 != 1970-01-02

      ======================================================================

            swhitesunw Seth White (Inactive)
            ovlasov Oleksandr Vlasov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: