How do I formate a java.sql Timestamp to my liking ? ( to a string, for display purposes)
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.
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);
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With