Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"

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

like image 947
user3388884 Avatar asked Oct 06 '22 21:10

user3388884


People also ask

How do you represent HH MM SS in Java?

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.

How do I get the current date timestamp?

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.

What is the format of timestamp in Java?

Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.


2 Answers

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());
like image 254
jmj Avatar answered Oct 09 '22 09:10

jmj


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.

like image 212
dimoniy Avatar answered Oct 09 '22 11:10

dimoniy