Java Timestamp example: How to create a “current timestamp” (i.e., now)

Java timestamp FAQ: When working with the Timestamp class, how do I create a “Java current timestamp,” i.e., a JDBC Timestamp object to represent the “current time” (i.e., now)?

Java Solution

You can create a “current time” JDBC Timestamp in just a few lines of code by using the Java Calendar class and a java.util.Date instance.

I show this in the three steps of the following example code, where I (a) get a Calendar instance, (b) get a Date from that instance, and then (c) get a Timestamp instance from that Date:

// 1) create a java calendar instance
Calendar calendar = Calendar.getInstance();

// 2) get a java.util.Date from the calendar instance.
//    this date will represent the current instant, or "now".
java.util.Date now = calendar.getTime();

// 3) a java current time (now) instance
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());

A more compact Java “Current Timestamp” example

I showed that example in three steps to be explicit about the steps needed to create a Timestamp object to represent the current time, but you can condense that code down to two steps, like this:

Calendar calendar = Calendar.getInstance();
java.sql.Date currentTimestamp = new java.sql.Timestamp(calendar.getTime().getTime());

or even one step, like this:

java.sql.Date currentTimestamp = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());

In either approach, those are the steps required to create a Java Timestamp object to represent the current date and time.

If all you needed to know was how to construct a timestamp with the current date and time, that may be all you need. But, if you’d like to see how to insert this Timestamp object into the timestamp field of a SQL database table using a JDBC Statement or PreparedStatement, I’ll show that next.

Inserting a current Timestamp into a database timestamp field

The following example demonstrates every step you need to create a Timestamp object, and then insert that object into the timestamp field of a SQL database table, using a SQL INSERT statement and a Java PreparedStatement:

import java.sql.*;
import java.util.Calendar;

public class JavaTimestampCurrentTimestampExample
  public static void main(String[] args) throws Exception
    // (1) connect to the database (mysql)
    String myDriver = "";
    String myUrl = "jdbc:mysql://localhost/date_time_tests";
    Connection connection = DriverManager.getConnection(myUrl, "root", "");

    // (2) create a java timestamp object that represents the current time (i.e., a "current timestamp")
    Calendar calendar = Calendar.getInstance();
    java.sql.Timestamp ourJavaTimestampObject = new java.sql.Timestamp(calendar.getTime().getTime());
    // (3) create a java timestamp insert statement
    String sqlTimestampInsertStatement = "INSERT INTO datetests (timestamp2) VALUES (?)";
    PreparedStatement preparedStatement = connection.prepareStatement(sqlTimestampInsertStatement);
    preparedStatement.setTimestamp(1, ourJavaTimestampObject);

    // (4) execute the sql timestamp insert statement, then shut everything down


I tried to comment the example fairly well, but as a quick discussion, here’s how this example works:

  1. The first thing to do is connect to the test database. For this test program, I’m using a MySQL database that is running on my location workstation.
  2. Next, I construct a Java SQL Timestamp object that represents the current date and time.
  3. After that, I create a SQL INSERT statement, and then construct a Java PreparedStatement using that INSERT statement. I use the setTimestamp method of the PreparedStatement class to specify that I want to insert my “current timestamp” object as the first parameter of my SQL INSERT statement.
  4. Finally, I insert the timestamp into the database with the PreparedStatement executeUpdate method, then close my PreparedStatement, and exit the program.

The database table timestamp fields

To fully understand that example, it helps to see the database design. Here’s the database schema for my datetests database table — including one date and two timestamp fields — which I created in a MySQL database:

create table datetests (
  date1 date,
  timestamp1 timestamp,
  timestamp2 timestamp

You may have seen earlier in the code that my SQL INSERT statement referred to the timestamp2 field. As you can see from the database schema, I do indeed have two timestamp fields in my table, timestamp1 and timestamp2. As you can guess, there is a method to my madness, which you’ll see next.

Results after the SQL Timestamp INSERT statement

When I log into my MySQL database and run a SELECT statement to see the record I just inserted, I see the following output:

mysql> select * from datetests;
| date1      | timestamp1          | timestamp2          |
| NULL       | 2009-10-02 16:52:30 | 2009-10-02 16:52:30 | 

The timestamp2 field contains the information from the Java Timestamp object I created and then inserted. If you’re not familiar with MySQL, you may be wondering where the timestamp in the timestamp1 column came from.

I don’t know if this behavior is similar in other databases, but in MySQL, if I skip the first timestamp field with my SQL INSERT statement, it defaults to the current date and time. I’d get the exact same result if I defined this timestamp database field like this:

timestamp1 timestamp not null default now()

Knowing this going into this test, I decided to create two timestamp fields so I could compare the current timestamp field generated by MySQL (timestamp1) and the current timestamp field generated from my Java Timestamp object and my corresponding SQL INSERT statement (timestamp2). As you can see, the two fields contain the same data.

This behavior is one reason I often skip timestamp fields when I insert a record into a SQL database. If I know the timestamp field is set to default to the current time, I’ll skip it, especially if I know that I’m logging an “event time” or “last modified by” field.

Java Timestamp references

I hope you’ve found this Java “current timestamp” tutorial helpful. Personally, I can never remember how to create a Timestamp to represent the current time (aka, “now”), so I refer to this article whenever I do this.

For more information on any of the JDBC Date, Time, and Timestamp classes, here are some links to the Sun Javadoc pages for these Java SQL classes: