How to format a java.sql Timestamp for displaying?

JavaDatetimeFormatting

Java Problem Overview


How do I formate a java.sql Timestamp to my liking ? ( to a string, for display purposes)

Java Solutions


Solution 1 - Java

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);

Solution 2 - Java

Use String.format (or java.util.Formatter):

Timestamp timestamp = ...
String.format("%1$TD %1$TT", timestamp)

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". 

Solution 3 - Java

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() );

Solution 4 - Java

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).

Solution 5 - Java

Use a DateFormat. In an internationalized application, use the format provide by getInstance. If you want to explicitly control the format, create a new SimpleDateFormat yourself.

Solution 6 - Java

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.

Solution 7 - Java

String timeFrSSHStr = timeFrSSH.toString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnosView Question on Stackoverflow
Solution 1 - JavaChssPly76View Answer on Stackoverflow
Solution 2 - Javauser85421View Answer on Stackoverflow
Solution 3 - JavaDreadPirateShawnView Answer on Stackoverflow
Solution 4 - JavaBrian AgnewView Answer on Stackoverflow
Solution 5 - JavaericksonView Answer on Stackoverflow
Solution 6 - JavaOle V.V.View Answer on Stackoverflow
Solution 7 - Java1909View Answer on Stackoverflow