Get the current timestamp in Java

You can get the Timestamp in Java in various ways, let's see some examples,

Example 1: Using Date class (java.util.Date)

Date date = new Date(System.currentTimeMillis());

Output: Sat Jul 24 10:55:39 PST 2021

Example 2: Using Date class (java.sql.Date)

The default Date constructor from java.sql package only prints out the date in yyyy-MM-dd format,

Date date = new Date(System.currentTimeMillis());


Example 3: Using Timestamp class

Timestamp class from java.sql package extends the java.util.Date class,

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

Output: 2021-07-25 13:51:48.021

Java Get Timestamp example
Java Get Timestamp example

Read more:

Have Questions? Post them here!

Recent Posts:

Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.