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

Date and Timestamp valueOf wrong for BCE years

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      java.sql.Date.valueOf(java.time.LocalDate) and java.sql.Timestamp.valueOf(java.time.LocalDateTime) return incorrect values when the year is BCE.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Save any negative date to an SQL database that supports it.
      Extract the date, then convert it to LocalDate. See test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Date sent to database: -0200-04-04
      Date returned from database: -0200-04-04
      Timestamp sent to database: -0200-04-04T10:30
      Timestamp returned from database: -0200-04-04T10:30

      ACTUAL -
      Date sent to database: -0200-04-04
      Date returned from database: 0201-04-04
      Timestamp sent to database: -0200-04-04T10:30
      Timestamp returned from database: 0201-04-04T10:30


      ---------- BEGIN SOURCE ----------
      import java.sql.Date;
      import java.sql.Timestamp;
      import java.time.LocalDate;
      import java.time.LocalDateTime;

      public class SqlDateTest {
      public static void main(String[] args) {
      LocalDate startDate = LocalDate.of(-200,4,4);
      Date sqlDate = Date.valueOf(startDate);
      LocalDate dateFromDatabase = sqlDate.toLocalDate();
      LocalDateTime startStamp = LocalDateTime.of(-200,4,4,10,30,0);
      Timestamp sqlStamp = Timestamp.valueOf(startStamp);
      LocalDateTime stampFromDatabase = sqlStamp.toLocalDateTime();
      System.out.println("Date sent to database: " + startDate);
      System.out.println("Date returned from database: " + dateFromDatabase);
      System.out.println("Timestamp sent to database: " + startStamp);
      System.out.println("Timestamp returned from database: " + stampFromDatabase);
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always

            lancea Lance Andersen
            tongwan Andrew Wang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: