groovy
  1. groovy
  2. GROOVY-4986

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

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.6.9, 1.7.10, 1.8.0
    • Fix Version/s: 1.8.5, 2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
      None
    • Environment:
      This bug appears to be platform and JDK independant.
    • Patch Submitted:
      Yes
    • Number of attachments :
      0

      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();
          }
      

        Activity

        Hide
        blackdrag blackdrag added a comment -

        fixed code tags

        Show
        blackdrag blackdrag added a comment - fixed code tags
        Hide
        blackdrag blackdrag added a comment -

        actually I think adding

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

        is a better way than an instanceof check in the method

        Show
        blackdrag blackdrag added a comment - actually I think adding public static Timestamp plus(Timestamp self, int days) { Calendar calendar = (Calendar) Calendar.getInstance().clone(); calendar.setTime(self); calendar.add(Calendar.DAY_OF_YEAR, days); Timestamp ts = new Timestamp(calendar.getTime().getTime()); ts.setNanos(self).getNanos()); return ts; } is a better way than an instanceof check in the method
        Hide
        Jim Driscoll added a comment -

        It's a matter of taste, but since there's already a separate method for java.sql.Date, I'm inclined to agree with you.

        Show
        Jim Driscoll added a comment - It's a matter of taste, but since there's already a separate method for java.sql.Date, I'm inclined to agree with you.

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Jim Driscoll
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: