Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a java.sql Timestamp for displaying?

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

like image 282
nos Avatar asked Jul 20 '09 23:07

nos


People also ask

How do I display time stamp in Java?

To get the current timestamp in Java, we can use the Timestamp class. Since this class does not have a default constructor, so we pass the time in milliseconds. We use the currentTimeMillis() method of System class to get the time.

How do I cast Java SQL timestamp to Java Lang string?

String dateString = "2016-02-03 00:00:00.0"; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss. S"). parse(dateString); String formattedDate = new SimpleDateFormat("yyyyMMdd"). format(date);

How do I change the date format on a timestamp?

To represent the date-only, use LocalDate class. LocalDate ld = LocalDate. parse( "2019-01-23" ) ; // By default, parses strings in standard ISO 8601 format: YYYY-MM-DD. To capture the current date, specific a time zone.


1 Answers

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); 
like image 96
ChssPly76 Avatar answered Oct 07 '22 23:10

ChssPly76