Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:25:56+00:00 2026-06-06T22:25:56+00:00

The java.util.Date toString() method displays the date in the local time zone. There are

  • 0

The java.util.Date toString() method displays the date in the local time zone.

There are several common scenarios where we want the data to be printed in UTC, including logs, data export and communication with external programs.

  • What’s the best way to create a String representation of java.util.Date in UTC?
  • How to replace the j.u.Date’s toString() format, which isn’t sortable (thanks, @JonSkeet!) with a better format?

Addendum

I think that the standard way of printing the date in a custom format and time zone is quite tedious:

final Date date = new Date();
final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);
System.out.println(sdf.format(date));

I was looking for a one-liner like:

System.out.println(prettyPrint(date, "yyyy-MM-dd'T'HH:mm:ss.SSS zzz", "UTC"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:25:59+00:00Added an answer on June 6, 2026 at 10:25 pm

    Following the useful comments, I’ve completely rebuilt the date formatter. Usage is supposed to:

    • Be short (one liner)
    • Represent disposable objects (time zone, format) as Strings
    • Support useful, sortable ISO formats and the legacy format from the box

    If you consider this code useful, I may publish the source and a JAR in github.

    Usage

    // 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

    (This code is also the subject of a question on Code Review stackexchange)

    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 <adam@matan.name>
     * @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.
         * 
         * <pre>
         * <code>
         * 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));
         * >>> UTCDate.toString moon:       1969-08-20T20:18:00.209 UTC
         * </code>
         * </pre>
         * 
         * @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.
         * 
         * <pre>
         * <code>
         * System.out.println(UTCDate.toLegacyString(new Date()));
         * >>> "Tue Jul 03 07:33:57 UTC 2012"
         * </code>
         * </pre>
         * 
         * @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.
         * 
         * <pre>
         * <code>
         * 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")
         * >>> "1969-08-20"
         * </code>
         * </pre>
         * 
         * 
         * @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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

By default, what time zone does method java.util.Date.toString() display? Since a Java Date stores
java.util.Date date = new java.util.Date(); java.sql.Date today = new java.sql.Date(date.getTime()); //2012-03-23 java.sql.Time time =
I have an application that passes in java.util.Date. I want to check whether this
I've got a question about SimpleDateFormat class and the java.util.Date's compareto method: I'm constructing
java.util.Date gets stored as 2010-09-03 15:33:22.246 when the SQL data type is timestamp, how
java.util.date = org.apache.commons.lang.time.DateUtils.parseDate(value, new String[] { mm/dd/yyyy }); If you give an invalid date
I'm looking for a simple method of converting between java.util.Date and javax.xml.datatype.XMLGregorianCalendar in both
I have a class DurationFormatter as below: import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Period; import
java.util.Date , java.util.Timetamp were seems to be causing great confusion for many. Within StackOverflow
I have two Java instances of java.util.Date and I have to find out if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.