Java current Timestamp FAQ: When working with a Timestamp in Java, how do I create a Java current timestamp, i.e., a JDBC Timestamp object to represent the "current time" ("now")?
Solution: You can create a "current time" JDBC Timestamp -- a Java SQL Timestamp -- in just a few lines of code, using the Java Calendar class and a java.util.Date instance, as shown in this example code:
// create a java 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(); // a java current time (now) instance java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
I showed that long example to be explicit, but you can condense that current Timestamp example 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 (i.e., now).
If all you needed to know was how to construct a Java current timestamp, that may be all you need. But, if you'd like to see how to insert this current timestamp object into the timestamp field of a SQL database table using a Java Statement or PreparedStatement, I'll show that next.
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 create a Java current timestamp object, and then insert that object 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 "current timestamp" example fairly well, but as a quick review, here's how this Java 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 helps 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 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 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 current 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