title: UTC Timestamp + Joda Time
body: <p>I am trying to get the UTC TimeStamp in a simple Java program using Joda:</p>
  <pre><code>public Timestamp getCurrentUTC(LocalDateTime date, DateTimeZone srcTZ, DateTimeZone dstTZ, Locale l) {
      DateTime srcDateTime = date.toDateTime(srcTZ);
      DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
      
      System.out.println(&quot;UTC Time:&quot; + dstDateTime.getMillis());      
      System.out.println(&quot;UTC Time:&quot; + new Timestamp(dstDateTime.getMillis()));
      
      return new Timestamp(dstDateTime.getMillis());
  }
  </code></pre>
  <p>The output of the program is as follows:</p>
  <pre><code>UTC Time:1378265162047
  UTC Time:2013-09-03 23:26:02.047
  </code></pre>
  <p>The millisecond value is the correct UTC time (i.e. confirmed with <code>GMT-4</code> timezone)
  The second value is the <code>EST</code> timezone.</p>
  <p>What I need is the UTC value unchanged as <code>java.sql.Timestamp</code> (ie TZ independent),
  for a database write. Is this possible?</p>
  <h1>Edit 1</h1>
  <pre><code>DateTime srcDateTime = date.toDateTime(srcTZ);
  
  DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
  
  System.out.println(&quot;UTC Time:&quot; + dstDateTime.getMillis());
  </code></pre>
  <p>I know that <code>srcDateTime</code> is the local date (GMT-4), and <code>dstDateTime</code> is
  UTC (GMT-0). Output values of the dates are as follows:</p>
  <pre class="lang-none prettyprint-override"><code>Source Date:2013-09-04T09:10:43.683-04:00
  
  Destination Date: 2013-09-04T13:10:43.683Z
  </code></pre>
  <p>I tried all the combinations to try to get the UTC value of <code>dstDateTime</code> as a java.sql.TimeStamp:</p>
  <pre><code>System.out.println(&quot;UTC Time:&quot; + dstDateTime.getMillis());
      
  System.out.println(&quot;UTC Time:&quot; + new Timestamp(srcDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
  
  System.out.println(&quot;UTC Time:&quot; + new Timestamp(dstDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
  </code></pre>
  <p>The print output for testing:</p>
  <pre class="lang-none prettyprint-override"><code>UTC Time:1378298760226 - Correct UTC
  
  UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
  
  UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
  </code></pre>
  <p>The first print line is the correct UTC timestamp. All I need is that same value
  as type java.sql.TimeStamp. Anything I tried always returned the local date time
  of the machine.</p>
  <h1>Edit 2</h1>
  <p>I tried the following:</p>
  <pre><code>System.out.println(&quot;UTC Timestamp:&quot; + date.toDateTime(srcTZ).getMillis());
  System.out.println(&quot;UTC Timestamp:&quot; + new Timestamp(date.toDateTime(srcTZ).getMillis()));
  </code></pre>
  <p>The output is as follows:</p>
  <pre class="lang-none prettyprint-override"><code>UTC Time:1378342856315 - Correct UTC Time
  UTC Timestap:2013-09-04 21:00:56.315 - Local Time other than the expected UTC Time
  </code></pre>
  <p>Whenever I try to convert to TimeStamp, I loose the valid UTC value that I am after.</p>
  <p>In terms of the method's parameters:</p>
  <pre><code>srcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone(&quot;America/Montreal&quot;)
  dstTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone(&quot;Etc/UTC&quot;))
  Local l = new Locale(&quot;en&quot;, &quot;CA&quot;)
  </code></pre>
  <p>Any help is greatly appreciated.</p>
  <p>Nick.</p>
  <h1>Edit 3</h1>
  <p>Hello Matt,</p>
  <p>Thank you so much for your response. We are getting the same results as you. Did not know the thing about printing etc.. More specifically:</p>
  <pre><code>System.out.println(&quot;UTC Timestamp:&quot; + srcDateTime.toDateTime(dstTZ).getMillis());
  System.out.println(&quot;UTC Timestamp:&quot; + srcDateTime.toDateTime(dstTZ));
  System.out.println(&quot;UTC Timestamp:&quot; + new Timestamp(srcDateTime.toDateTime(dstTZ).getMillis()));
  </code></pre>
  <p>Yields The Output:</p>
  <pre class="lang-none prettyprint-override"><code>UTC Timestamp:1378389098468 - Correct UTC Timestap (Thu, 05 Sep 2013 13:51:38 GMT)
  UTC Timestamp:2013-09-05T13:51:38.468Z - Correct UTC Time
  UTC Timestamp:2013-09-05 09:51:38.468 - Local time is printed, UTC is expected
  </code></pre>
  <p>The problem was brought to my attention when we realized that the DB was storing the local time instead of UTC:</p>
  <pre class="lang-none prettyprint-override"><code>+---------------------+
  | effectivedate       |
  +---------------------+
  | 2013-09-05 09:34:11 |
  +---------------------+
  </code></pre>
  <p>The Mysql timezone is set to '-00:00'</p>
  <pre class="lang-none prettyprint-override"><code>mysql&gt; SELECT CURRENT_TIMESTAMP;
  +---------------------+
  | CURRENT_TIMESTAMP   |
  +---------------------+
  | 2013-09-05 13:48:09 |
  +---------------------+
  </code></pre>
  <p>Debugging the application using eclipse debugger we realized that the local date time (2013-09-05 09:51:38.468) was being passed to the DB (Can't post images, not enough points...). The datatype is straight TimeStamp, with no string manipulation. Maybe the eclipse debugger is using <code>String.println()</code> function as well, not sure..</p>
  <p>I really appreciate all the help debugging our application. Did not want to take up so much time (no pun intended) and effort...</p>
  <p>Kind Regards,</p>
  <p>Nick.</p>