Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

java.sql.Timestamp extends java.util.Date . You can do:

String s = new SimpleDateFormat("MM/dd/yyyy").format(myTimestamp);

Or to also include time:

String s = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(myTimestamp);
                Just a small reminder that if SimpleDateFormat object is a local-scoped object (it is created and used only inside a method), then it is thread-safe, since stack on which it will reside is inherently "thread-safe" (as it belongs to a single thread).
– quantum
                Sep 13, 2011 at 9:52
                To include time you will need to use SimpleDateFormat("MM/dd/yyyy hh:mm") or something similar
– AverageMarcus
                Jul 26, 2012 at 14:01
                to make it complete for those who needs it, String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(myTimestamp);
– Ishan Liyanage
                Oct 3, 2013 at 9:17

EDIT:
please see the documentation of Formatter to know what TD and TT means: click on java.util.Formatter

The first 'T' stands for:

't', 'T'    date/time   Prefix for date and time conversion characters.

and the character following that 'T':

'T'     Time formatted for the 24-hour clock as "%tH:%tM:%tS".
'D'     Date formatted as "%tm/%td/%ty". 
                This is the best answer, according to me. No superfluous object is created just for converting the timestamp to a string.
– Salil
                May 25, 2013 at 3:33
                Very elegant. Just to be clear, the formatting produced by the example here looks like 05/30/17 00:39:18.
– Noumenon
                May 30, 2017 at 4:41

If you're using MySQL and want the database itself to perform the conversion, use this:

DATE_FORMAT(date,format)

If you prefer to format using Java, use this:

java.text.SimpleDateFormat

SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
dateFormat.format( new Date() );

For this particular question, the standard suggestion of java.text.SimpleDateFormat works, but has the unfortunate side effect that SimpleDateFormat is not thread-safe and can be the source of particularly nasty problems since it'll corrupt your output in multi-threaded scenarios, and you won't get any exceptions!

I would strongly recommend looking at Joda for anything like this. Why ? It's a much richer and more intuitive time/date library for Java than the current library (and the basis of the up-and-coming new standard Java date/time library, so you'll be learning a soon-to-be-standard API).

Good warning. But very easy to address - don't share SimpleDateFormat instances by storing them in session scope / instance variables / static context and then accessing them from multiple threads. Just create a new SimpleDateFormat() within your method and discard it after use. That is threadsafe. – Glen Best Apr 24, 2013 at 4:48 Of course if you really want to store/share SimpleDateFormat instance, then synchronise access: synchronized(simpleDateFormatInstance) {s = simpleDateFormatInstance.format(myTimeStamp)} OR create a custom extension of SDF class, and do this automatically within the format method. – Glen Best Apr 24, 2013 at 4:55

java.time

I am providing the modern answer. The Timestamp class is a hack on top of the already poorly designed java.util.Date class and is long outdated. I am assuming, though, that you are getting a Timestamp from a legacy API that you cannot afford to upgrade to java.time just now. When you do that, convert it to a modern Instant and do further processing from there.

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(Locale.GERMAN);
    Timestamp oldfashionedTimestamp = new Timestamp(1_567_890_123_456L);
    ZonedDateTime dateTime = oldfashionedTimestamp.toInstant()
            .atZone(ZoneId.systemDefault());
    String desiredFormat = dateTime.format(formatter);
    System.out.println(desiredFormat);

Output in my time zone:

07.09.2019 23:02:03

Pick how long or short of a format you want by specifying FormatStyle.SHORT, .MEDIUM, .LONG or .FULL. Pick your own locale where I put Locale.GERMAN. And pick your desired time zone, for example ZoneId.of("Europe/Oslo"). A Timestamp is a point in time without time zone, so we need a time zone to be able to convert it into year, month, day, hour, minute, etc. If your Timestamp comes from a database value of type timestamp without time zone (generally not recommended, but unfortunately often seen), ZoneId.systemDefault() is likely to give you the correct result. Another and slightly simpler option in this case is instead to convert to a LocalDateTime using oldfashionedTimestamp.toLocalDateTime() and then format the LocalDateTime in the same way as I did with the ZonedDateTime.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.