Is it possible to convert/format java.sql.Timestmap
into a String that has the following format:
yyyyMMdd
I know that doing it with a String
is fairly simple e.g:
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);
But, I have to work with the Timestamp
object.
The java.sql.Timestamp
extends java.util.Date
, so you can format it exactly the same way:
String formattedDate = new SimpleDateFormat("yyyyMMdd").format(date);
Parsing is almost the same, you can construct it using its "millis" representation:
Timestamp timestamp = new Timestamp(new SimpleDateFormat("yyyyMMdd").parse(formattedDate).getTime());
You could do something like so:
Timestamp ts = ...;
Date date = new Date();
date.setTime(ts.getTime());
String formattedDate = new SimpleDateFormat("yyyyMMdd").format(date);
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