title: Groovy: Date and Time comparisons with a slight delay
body: <p>So I have the following script:</p>
  <pre><code>import groovy.time.TimeCategory
  
  def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
  
  def date = new Date(messageExchange.getTimestamp())
  
  use(groovy.time.TimeCategory){
      after24Hours = (date + 24.hours).format(&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;, TimeZone.getTimeZone('UTC')) }
  
  assert dueDate == after24Hours
  </code></pre>
  <p>What I'm trying to do with this is take the date and time from a REST request (dueDate - which comes in UTC format and with a 24h delay) and create a new date and time from the timestamp of the moment when that request has been sent, which is registered from my system. I then convert that time to UTC to accommodate the format from dueDate and add 24h to it. At the end I verify that the date and time from dueDate and after24Hours is the same.</p>
  <p>The output does return the same time but in certain cases if there is a delay between the time the request is being sent and the time is received then the assertion will fail. This depends on the server, usually there is a difference of like 1 millisecond but I'm thinking that if the server will be slower at some point this will definitely be bigger.</p>
  <p>What could I do to allow some margin of error in the assertion, maybe like a few seconds or even a couple of minutes?</p>
  <p>Ok, so I managed to do this:</p>
  <pre><code>import groovy.time.*
  
  def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
  def date = new Date(messageExchange.getTimestamp())
  
  use(groovy.time.TimeCategory){
      after24Hours = (date + 24.hours).format(&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;, TimeZone.getTimeZone('UTC'))
       def date1 = Date.parse(&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;, dueDate)
       def date2 = Date.parse(&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;, after24Hours)
       TimeDuration difference = TimeCategory.minus(date2, date1) 
       log.info date1
       log.info date2
       assert difference &lt; 2.minutes
       }
  </code></pre>
  <p>The script seems to work and it does return an error only if the time is longer than the one I've set in the assertion.
  Unfortunately I have another issue now.
  For some reason, my date output looks like this:</p>
  <pre><code>Fri Oct 01 16:24:10 EEST 2021: INFO: Sat Oct 02 13:24:10 EEST 2021
  </code></pre>
  <p>Which is not the correct format. That date should appear in the Zulu format, after all when I parsed the dates that was the format that I used.
  Am I missing something?</p>