title: How to efficiently calculate duration between two timestamps including date in java?
body: <p>I have two time stamps which is in UTC with format <code>2021-05-19T03:03:55.095Z</code>  and <code>2021-05-19T05:10:48.177Z</code> . How can i calculate duration between these two in minutes even when there is date change happened. For example date of second time stamp changed to 2021-05-20T05:10:48.177Z.
  Below are the few parser which i have done can anyone help me with this.</p>
  <pre><code>private String timeParser (String timestamp){
      String output = &quot;&quot;;
      DateFormat df = new SimpleDateFormat(&quot;yyyy-MM-dd'T'HH:mm:ss.SSS'Z'&quot;);
      try {
          Date date1 = df.parse(timestamp);
          DateFormat outputFormatter1 = new SimpleDateFormat(&quot;yyyy-MM-dd hh:mm:ss&quot;);
          output = outputFormatter1.format(date1); //
      } catch (ParseException e) {
          logger.error(e);
      }
      return output;
  }
  
  
   public void calculateDuration(JsonNode aggEvent, JsonNode newEvent){
      Timestamp createdTimestamp = Timestamp.valueOf(timeParser(&quot;2021-05-19T03:03:55.095Z&quot;));
      Timestamp newTimestamp = Timestamp.valueOf(timeParser(&quot;2021-05-20T05:10:48.177Z&quot;));
      float timeDifference = newTimestamp.getTime() - createdTimestamp.getTime();
      float minutesDifference = (timeDifference / (1000 * 60)) % 60;
      System.out.println(Math.round(minutesDifference * 100.0) / 100.0);
  }
  </code></pre>
  <p>Can someone correct here if im doing anything wrong?</p>