Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4986

java.sql.Timestamp + int = java.util.Date

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.6.9, 1.7.10, 1.8.0
    • 1.8.5, 2.0-beta-2
    • groovy-jdk
    • None
    • This bug appears to be platform and JDK independant.
    • Patch

    Description

      The following code:

      def d = new Date()
      def t = new java.sql.Timestamp(d.getTime())
      println t.class
      t = t + 1
      println t.class
      

      Produces the following output:

      class java.sql.Timestamp
      class java.util.Date

      This behavior is unexpected, and is causing us a small problem. The fix would be to change the org.codehaus.groovy.runtime.DateGroovyMethods.plus(Date,int) method to be as follows:

          public static Date plus(Date self, int days) {
              Calendar calendar = (Calendar) Calendar.getInstance().clone();
              calendar.setTime(self);
              calendar.add(Calendar.DAY_OF_YEAR, days);
              if (self instanceof java.sql.Timestamp) {
                  java.sql.Timestamp ts = new java.sql.Timestamp(calendar.getTime().getTime());
                  ts.setNanos(((java.sql.Timestamp)self).getNanos());
                  return ts;
              }
              return calendar.getTime();
          }
      

      Attachments

        Activity

          People

            roshandawrani Roshan Dawrani
            driscoll Jim Driscoll
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: