How to get current timestamp in Java?

To get current timestamp in Java, do this: new java.sql.Timestamp(System.currentTimeMillis()).

Here's how you do it:

java.sql.Timestamp current = new java.sql.Timestamp(System.currentTimeMillis());

System.out.println(current); // YYYY-MM-DD HH:MM:SS.MMM

As date

Here's how you do it with a Date object:

java.util.Date current = new java.util.Date();

System.out.println(current); // DAY MMM DD HH:MM:SS ZONE YYYY

As string

Here's how to format the current timestamp to a String:

String current = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());

System.out.println(current); // YYYY-MM-DD HH:MM:SS