All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Current Timestamp In Java

Last Updated : Jul 1, 2023

How To Get Current Timestamp In Java

In this article we will show you the solution of how to get current timestamp in java, the System.currentTimeMillis() function of the Timestamp class can be used to instantiate a fresh instance to get the current timestamp in Java.

A current time since January 1, 1970, 00:00:00 GMT, as returned by the System.currentTimeMillis() method.

By using this value, a fresh Timestamp object that represents the present timestamp can be created.

We'll talk about the idea of getting the current timestamp in Java now.

Step By Step Guide On How To Get Current Timestamp In Java :-

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeStampExample {
    private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
    private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    private static final SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println(timestamp);
        Date date = new Date();
        System.out.println(new Timestamp(date.getTime()));
        System.out.println(timestamp.getTime());
        System.out.println(sdf1.format(timestamp));
        System.out.println(sdf2.format(timestamp));
        System.out.println(sdf3.format(timestamp));
    }
}
  1. You can see that we created a straightforward Java programme to show how to alter dates and timestamps using the SimpleDateFormat class and the Timestamp class.
  2. We import the relevant classes, Timestamp, SimpleDateFormat, and Date, at the beginning of the code.
  3. Next, the programme creates the sdf1, sdf2, and sdf3 SimpleDateFormat objects.
  4. The timestamp can be formatted in a variety of ways using these objects.
  5. The timestamp for sdf1 is formatted in the manner of "year.month.day.hour.minute.second" using the format "yyyy.MM.dd.HH.mm.ss".
  6. The timestamp is formatted in ISO-8601 by sdf2, which uses the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format.
  7. For sdf3, the timestamp is formatted in the manner of "year-month-day hour:minute:second" using the "yyyy-MM-dd HH:mm:ss" format.
  8. A new Timestamp object is subsequently created in the main method using the current system time.
  9. The system time in milliseconds is then returned by the System.currentTimeMillis() method, which is used to build a new Timestamp object.
  10. The System.out.println() function is used to print this timestamp to the console.
  11. The date is then set to the current system time when a new Date object is created using the default constructor.
  12. Utilising the getTime() method of this Date object, which returns the milliseconds since January 1, 1970, 00:00:00 GMT, a new Timestamp object is then produced.
  13. The System.out.println() function is also used to print this timestamp to the console.
  14. In order to determine how many milliseconds have passed since January 1, 1970, 00:00:00 GMT, the getTime() method is then invoked on the original timestamp object.
  15. The console displays this value.
  16. Utilising the three SimpleDateFormat objects that were previously constructed, the timestamp object is then formatted.
  17. In the final statement, System.out.println() is used to print the formatted timestamps to the console.

Conclusion :-

As a result, we have successfully mastered the Java concept of getting the current timestamp.

We also discovered that this Java programme shows how to alter dates and timestamps using the SimpleDateFormat and Timestamp classes.

Using the SimpleDateFormat class, it produces a new Timestamp object, formats it in various ways, and outputs the results to the console.

I hope this article on how to get current timestamp in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪