Java Timestamp Example: When working with a Timestamp in Java, how do I create a JDBC Timestamp object to represent the "current time", i.e., "now"?
Solution: You can create a Java Timestamp object (a JDBC Timestamp) to represent the current time in just a few lines of code, using the Calendar class and a java.util.Date instance, as shown in this example code:
// create a calendar instance Calendar calendar = Calendar.getInstance(); // get a java.util.Date from the Calendar instance. // this date will represent the current instant, or "now". java.util.Date now = calendar.getTime(); // create a JDBC Timestamp instance java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
I showed that long example to be explicit, but you can condense that code, like this:
Calendar calendar = Calendar.getInstance(); java.sql.Date currentTimestamp = new java.sql.Timestamp(calendar.getTime().getTime());
or even 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 ("now").
If all you needed to know was how to construct a "current timestamp" object, that may be all you need to see. But if you'd like to see how to insert this Java Timestamp object into the timestamp field of a SQL database table using a Java Statement or PreparedStatement, I'll demonstrate that next.
If you just wanted to see how to construct a java.sql.Timestamp object to represent the current date and time, the Java code shown above may be all you need to know. But, if you want to see how to use a current timestamp object in a complete SQL INSERT statement and a Java PreparedStatement, the following complete example program demonstrates every step you need to insert a Java Timestamp into the timestamp field of a SQL database table:
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 = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/date_time_tests";
Class.forName(myDriver);
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 our 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
preparedStatement.executeUpdate();
preparedStatement.close();
System.exit(0);
}
}
I've tried to comment this "insert current timestamp" example fairly well, but as a quick review, here's how this Java timestamp example works:
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.executeUpdate method, then close my PreparedStatement, and exit the program.To fully understand that current Timestamp example, it will help to see the database design. Here's the database schema for my datetests database table, 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.
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 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 will skip it, especially if I know that I'm logging an "event time" or "last modified by" field.
I hope you've found this Java Timestamp tutorial helpful.
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:
Nice article.
Nice article.
Post new comment