Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Following the useful comments, I've completely rebuilt the date formatter. Usage is supposed to:</p> <ul> <li>Be short (one liner)</li> <li>Represent disposable objects (time zone, format) as Strings</li> <li>Support useful, sortable ISO formats and the legacy format from the box</li> </ul> <p>If you consider this code useful, I may publish the source and a JAR in github.</p> <h3>Usage</h3> <pre class="lang-java prettyprint-override"><code>// The problem - not UTC Date.toString() "Tue Jul 03 14:54:24 IDT 2012" // ISO format, now PrettyDate.now() "2012-07-03T11:54:24.256 UTC" // ISO format, specific date PrettyDate.toString(new Date()) "2012-07-03T11:54:24.256 UTC" // Legacy format, specific date PrettyDate.toLegacyString(new Date()) "Tue Jul 03 11:54:24 UTC 2012" // ISO, specific date and time zone PrettyDate.toString(moonLandingDate, "yyyy-MM-dd hh:mm:ss zzz", "CST") "1969-07-20 03:17:40 CDT" // Specific format and date PrettyDate.toString(moonLandingDate, "yyyy-MM-dd") "1969-07-20" // ISO, specific date PrettyDate.toString(moonLandingDate) "1969-07-20T20:17:40.234 UTC" // Legacy, specific date PrettyDate.toLegacyString(moonLandingDate) "Wed Jul 20 08:17:40 UTC 1969" </code></pre> <h3>Code</h3> <p><em>(This code is also the subject of <a href="https://codereview.stackexchange.com/questions/13283/java-date-formatter">a question on Code Review stackexchange</a>)</em></p> <pre><code>import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; /** * Formats dates to sortable UTC strings in compliance with ISO-8601. * * @author Adam Matan &lt;adam@matan.name&gt; * @see http://stackoverflow.com/questions/11294307/convert-java-date-to-utc-string/11294308 */ public class PrettyDate { public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz"; public static String LEGACY_FORMAT = "EEE MMM dd hh:mm:ss zzz yyyy"; private static final TimeZone utc = TimeZone.getTimeZone("UTC"); private static final SimpleDateFormat legacyFormatter = new SimpleDateFormat(LEGACY_FORMAT); private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT); static { legacyFormatter.setTimeZone(utc); isoFormatter.setTimeZone(utc); } /** * Formats the current time in a sortable ISO-8601 UTC format. * * @return Current time in ISO-8601 format, e.g. : * "2012-07-03T07:59:09.206 UTC" */ public static String now() { return PrettyDate.toString(new Date()); } /** * Formats a given date in a sortable ISO-8601 UTC format. * * &lt;pre&gt; * &lt;code&gt; * final Calendar moonLandingCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); * moonLandingCalendar.set(1969, 7, 20, 20, 18, 0); * final Date moonLandingDate = moonLandingCalendar.getTime(); * System.out.println("UTCDate.toString moon: " + PrettyDate.toString(moonLandingDate)); * &gt;&gt;&gt; UTCDate.toString moon: 1969-08-20T20:18:00.209 UTC * &lt;/code&gt; * &lt;/pre&gt; * * @param date * Valid Date object. * @return The given date in ISO-8601 format. * */ public static String toString(final Date date) { return isoFormatter.format(date); } /** * Formats a given date in the standard Java Date.toString(), using UTC * instead of locale time zone. * * &lt;pre&gt; * &lt;code&gt; * System.out.println(UTCDate.toLegacyString(new Date())); * &gt;&gt;&gt; "Tue Jul 03 07:33:57 UTC 2012" * &lt;/code&gt; * &lt;/pre&gt; * * @param date * Valid Date object. * @return The given date in Legacy Date.toString() format, e.g. * "Tue Jul 03 09:34:17 IDT 2012" */ public static String toLegacyString(final Date date) { return legacyFormatter.format(date); } /** * Formats a date in any given format at UTC. * * &lt;pre&gt; * &lt;code&gt; * final Calendar moonLandingCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); * moonLandingCalendar.set(1969, 7, 20, 20, 17, 40); * final Date moonLandingDate = moonLandingCalendar.getTime(); * PrettyDate.toString(moonLandingDate, "yyyy-MM-dd") * &gt;&gt;&gt; "1969-08-20" * &lt;/code&gt; * &lt;/pre&gt; * * * @param date * Valid Date object. * @param format * String representation of the format, e.g. "yyyy-MM-dd" * @return The given date formatted in the given format. */ public static String toString(final Date date, final String format) { return toString(date, format, "UTC"); } /** * Formats a date at any given format String, at any given Timezone String. * * * @param date * Valid Date object * @param format * String representation of the format, e.g. "yyyy-MM-dd HH:mm" * @param timezone * String representation of the time zone, e.g. "CST" * @return The formatted date in the given time zone. */ public static String toString(final Date date, final String format, final String timezone) { final TimeZone tz = TimeZone.getTimeZone(timezone); final SimpleDateFormat formatter = new SimpleDateFormat(format); formatter.setTimeZone(tz); return formatter.format(date); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload