title: Comparing Java Dates and force a date to a particular time zone
body: <p>I am having trouble doing a date comparison.</p>
  
  <p>I am using Groovy and Spock to write an integration test against a web service.</p>
  
  <p>The test is to first use the web service to create a <code>Thing</code>, then immediately make another call to get the details of the <code>Thing</code> by its ID. I then want to verify that the <code>CreatedDate</code> of the thing is greater than a minute ago.</p>
  
  <p>Here is some JSON of the call</p>
  
  <pre><code>{
    "Id":"696fbd5f-5a0c-4209-8b21-ea7f77e3e09d",
    "CreatedDate":"2017-07-11T10:53:52"
  }
  </code></pre>
  
  <p>So, note no timezone information in the date string; but I know it is UTC.</p>
  
  <p>I'm new to Java (from .NET) and a bit bamboozled by the different date types.</p>
  
  <p>This is my Groovy model of the class, which I use Gson to deserialize:</p>
  
  <pre><code>class Thing {
      public UUID Id
      public Date CreatedDate
  }
  </code></pre>
  
  <p>The deserialization works fine. But the code, which runs in a non-UTC time zone thinks that the date is actually in the local time zone.</p>
  
  <p>I can create a variable representing "1 minute ago" using the <code>Instant</code> class:</p>
  
  <pre><code>def aMinuteAgo = Instant.now().plusSeconds(-60)
  </code></pre>
  
  <p>And this is how I am trying to do the comparison:</p>
  
  <pre><code>rule.CreatedDate.toInstant().compareTo(aMinuteAgo) &lt; 0
  </code></pre>
  
  <p>The trouble is, the runtime thinks that the date is local time. There appears to be no overload for me to force <code>.toInstant()</code> into UTC.</p>
  
  <p>I've tried using what I understand to be more modern classes - such as <code>LocalDateTime</code> and <code>ZonedDateTime</code> in my model instead of <code>Date</code>, however Gson doesn't play nice with the deserialization.</p>