title: Convert Java Date to UTC String
body: <p>The <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html" rel="noreferrer">java.util.Date</a> <code>toString()</code> method displays the date in the local time zone.</p>
  <p>There are several common scenarios where we want the data to be printed in <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time" rel="noreferrer">UTC</a>, including logs, data export and communication with external programs.</p>
  <ul>
  <li>What's the best way to create a String representation of <code>java.util.Date</code> in UTC?</li>
  <li>How to replace the j.u.Date’s <code>toString()</code> format, which isn't sortable (thanks, @JonSkeet!) with a better format?</li>
  </ul>
  <h3>Addendum</h3>
  <p>I think that the standard way of printing the date in a custom format and time zone is quite tedious:</p>
  <pre class="lang-java prettyprint-override"><code>final Date date = new Date();
  final String ISO_FORMAT = &quot;yyyy-MM-dd'T'HH:mm:ss.SSS zzz&quot;;
  final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
  final TimeZone utc = TimeZone.getTimeZone(&quot;UTC&quot;);
  sdf.setTimeZone(utc);
  System.out.println(sdf.format(date));
  </code></pre>
  <p>I was looking for a one-liner like:</p>
  <pre class="lang-java prettyprint-override"><code>System.out.println(prettyPrint(date, &quot;yyyy-MM-dd'T'HH:mm:ss.SSS zzz&quot;, &quot;UTC&quot;));
  </code></pre>