How to get timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());
This is what I have, but Timestamp() requires an parameters...
We can change the pattern in the SimpleDateFormat for the conversion. The pattern dd/MM/yyyy hh:mm:ss aa is used for the 12 hour format and the pattern MM-dd-yyyy HH:mm:ss is used for the 24 hour format. In this program we are changing the format of the date by changing the patterns and formatting the input date.
Get Current Timestamp Using the toInstant() Method in Java If we have a date object representing the timestamp, we can also use the toInstant() method of the Date class to get the current timestamp.
Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.
Replace
new Timestamp();
with
new java.util.Date()
because there is no default constructor for Timestamp
, or you can do it with the method:
new Timestamp(System.currentTimeMillis());
Use java.util.Date
class instead of Timestamp.
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
This will get you the current date in the format specified.
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