title: Checking elapsed time in Groovy/Grails
body: <p>I'm trying to get the duration of a web-based phone call by passing a new Date value to an application at the connection time and exit time of each call. </p>
  
  <p>The code looks a little bit like this:</p>
  
  <pre><code>class Call {
      Date startTime = new Date()
      Date endTime = new Date()
      String callLength = ""
      String Id = UUID.randomUUID()
  }
  class callController {
  
      def connected = {
           callInstance = Call.findById(params.Id)
  
           //Id params are passed through from external voice call 
           Call.executeUpdate("update Call a set a.startTime=? where a.Id='${params.id}'", [new Date()])
      }
      def callEnded = {
          callInstance = Call.findById(params.Id)
          Call.executeUpdate("update Call a set a.endTime=? where a.Id='${params.id}'", [new Date()])
          timeHandler(callInstance.endTime, callInstance.startTime)
      }
      def timeHandler = {end, start-&gt;
          TimeDuration duration = TimeCategory.minus(end, start)
          Call.executeUpdate("update Call a set a.callLength='$duration' where a.Id = '${params.id}'")
      }
  }
  </code></pre>
  
  <p>Edit: Code edited slightly to produce a better output.</p>
  
  <p>Edit 2:</p>
  
  <p>I created variables on the Call object for startTime and endTime. But, now I'm having trouble updating the dates-- Grails doesn't like to update dates, I guess? It says it's having trouble passing a new Date to a Date variable. This is the exact error:</p>
  
  <pre><code>java.lang.ClassCastException: [Ljava.util.Date; cannot be cast to java.util.Date
  </code></pre>
  
  <p>and the Stacktrace points to the line where I try to update the call. Is there a workaround for this?</p>
  
  <p>Thanks again!</p>
  
  <p>Edit 3:</p>
  
  <p>Fixed with correct code.</p>
  
  <p>Thank everyone for your help!</p>