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

java.sql.Timestamp.valueOf(String) fails to throw an IllegalArgumentException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6-pool
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP 5.1.2600

      A DESCRIPTION OF THE PROBLEM :
      Passing an invalid date to the Timestamp.valueOf should throw an illegalArgumentException, but this only occurs if the formatting is invalid. If the formatting is ok but the date is invalid, the valueof method succeeds and returns a date that does not match the original.

      2007-08-21 12:00:66 (66 secs) returns a timestamp of 2007-08-21 12:01:06.0 (1 min 12 seconds)
      2007-02-30 12:00:58 (30 feb) returns a timestamp of 2007-03-02 12:00:58.0 (2 march)

      For instance in the first case, 66 seconds is invalid. However instead of throwing an exception, this is translated to mean '1 minute and 16 seconds'. This allows inaccurate data to slip through unnoticed.

      According to the API:

      Throws:
          IllegalArgumentException - if the given argument does not have the format yyyy-mm-dd hh:mm:ss.fffffffff


      Since 72 is an invalid value for 'ss', an exception should be thrown.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please use the following, though obviously this code needs a 'main' and class definition etc

      import java.sql.Timestamp;

      private Timestamp startDate;
      try {
      startDate = Timestamp.valueOf("2007-08-21 12:00:66");
      } catch (IllegalArgumentException e) {
      System.err.println("startDate argument must be in 'yyyy-mm-dd hh:mm:ss' format");
                System.exit(0);
      }

      System.out.println(startDate)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An illegalArgumentException should be thrown
      ACTUAL -
      2007-08-21 12:00:66 (66 secs) returns a timestamp of 2007-08-21 12:01:06.0 (1 min 12 seconds)
      2007-02-30 12:00:58 (30 feb) returns a timestamp of 2007-03-02 12:00:58.0 (2 march)

      REPRODUCIBILITY :
      This bug can be reproduced always.

            lancea Lance Andersen
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: