All TalkersCode Topics

Follow TalkersCode On Social Media

Java Get Current Timestamp

Last Updated : Aug 7, 2023

Java Get Current Timestamp

In this article we will show you the solution of java get current timestamp, in addition to creating and updating files in your computer, sending and receiving emails, and so on, you assign a date and timestamp to each one.

The date and time are essential for every operation of a computer.

Application developers often need to know the current date or time while developing their applications.

With Java, you can set and adjust dates and times with the help of two packages: java.time and java.util.

We will now discuss the idea of how to get the current timestamp in java with an example.

Step By Step Guide On Java Get Current Timestamp :-

import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDateTime;
public class TCExample {
  private static final String SQL_INSERT = "INSERT INTO EMPLOYEE (NAME, SALARY, CREATED_DATE) VALUES (?,?,?)";
  public static void main(String[] args) {
      try (Connection conn = DriverManager.getConnection(
              "jdbc:postgresql://", "postgres", "password");
           PreparedStatement preparedStatement = conn.prepareStatement(SQL_INSERT)) {
          preparedStatement.setString(1, "TC");
          preparedStatement.setBigDecimal(2, new BigDecimal("799.88"));
          preparedStatement.setTimestamp(3, Timestamp.valueOf(;
          // preparedStatement.setTimestamp(3, Timestamp.from(;
          // preparedStatement.setTimestamp(3, Timestamp.from(;
          int row = preparedStatement.executeUpdate();
          // rows affected
          System.out.println(row); //1
      } catch (SQLException e) {
          System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
      } catch (Exception e) {
  1. We start by importing the necessary classes - java.math.BigDecimal for dealing with money, java.sql.* for interacting with the database, and java.time.LocalDateTime for getting the current date and time.
  2. Whenever new data are inserted into the EMPLOYEE table, we define a constant string consisting of the SQL query.
  3. This method establishes a connection to our PostgreSQL database by passing in the JDBC URL, username, and password defined in DriverManager.getConnection().
  4. The SQL query previously defined is used to create a PreparedStatement object. As a result, we are able to insert data into the database while also protecting our database from SQL injection attacks.
  5. By using the setString, setBigDecimal, and setTimestamp methods in the PreparedStatement object, we set the values for the three placeholders in the SQL query. As you can see, we are inserting the details for the employee, such as the employee's name, salary, and current date and time.
  6. A SQL query is executed using the executeUpdate method of the PreparedStatement object. To print out the number of rows affected by the query, we return the number of rows returned by the query.
  7. In the event of an exception occurring while interacting with the database, we print out the message in the console.

Conclusion :-

As a result, we have successfully learned how to get the current timestamp in java with an example.

A timestamp can be obtained by using the methods of the Date class, ZonedDateTime class, Instant class, or LocalDateTime class in Java.

The classes in this package are part of the package java.time and the package java.util.

In this case, they may use methods such as "now()", "format()", "pattern()", etc.

Through detailed examples in this manual, we have demonstrated all the methods to get the current timestamp in Java.

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

Latest Tutorials