Java “current date” example - How to create a Java Date to represent the current time

Java current date FAQ: When working with a Date in Java, how do I create a Java Date (java.sql.Date) object to represent the current date (i.e., "now")?

Java current date - solution

Solution: You can create a Java Date object to represent the current date in just a few lines of code, as shown in the following code:

 

 

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

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

// now, create a java.sql.Date from the java.util.Date
java.sql.Date date = new java.sql.Date(currentDate.getTime());

I showed that code fully expanded so you can see what you need to do, but once you understand that background, you can condense that code some more, like this:

// java.util.Date
java.util.Date currentDate = Calendar.getInstance().getTime();

// java.sql.Date
Calendar calendar = Calendar.getInstance();
java.sql.Date ourJavaDateObject = new java.sql.Date(calendar.getTime().getTime());

or for the java.sql.Date, even into one line, like this:

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

No matter which approach you take, those are the steps required to create a java.sql.Date to represent the current date and time (i.e., "now").

A complete "Java current date" example

If you just wanted to see how to construct a java.sql.Date object to represent the current date, that's probably all you need to know. But, if you want to see how to use that Java current date object in a complete SQL INSERT example, the following Java program demonstrates every step you need to insert the current date into the date field of a SQL database table, including the initial database connection:

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

public class JavaCurrentDateTimeExample
{
  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", "root");

    // (2) create a java sql date object we want to insert
    Calendar calendar = Calendar.getInstance();
    java.sql.Date ourJavaDateObject = new java.sql.Date(calendar.getTime().getTime());
    
    // (3) create our date insert statement
    String query = "INSERT INTO datetests (date1) VALUES (?)";
    PreparedStatement st = connection.prepareStatement(query);
    st.setDate(1, ourJavaDateObject);

    // (4) execute the insert statement, then shut everything down
    st.executeUpdate();
    st.close();
    System.exit(0);
  }
}

As you can see, this sample Java program shows how to connect to a MySQL database, construct a Java Date object, create a SQL INSERT statement, use that INSERT statement in a Java PreparedStatement, and then execute this SQL INSERT statement, which inserts our Java current date into the database.

The test database table

If you want to try to repeat this example on your system, here's the schema for the "current date" test database table that was used in our JavaCurrentDateTimeExample program:

create table datetests (
  date1 date
);

Confirming the current date in the database

After running my JavaCurrentDateTimeExample program, I logged into my MySQL database and checked my datetests database table to make sure it contained the current date with this SQL query:

mysql> select date1 from datetests;
+------------+
| date1      |
+------------+
| 2009-10-02 | 
+------------+
1 row in set (0.00 sec)

As you can see, this "current date" approach works just fine.

JDBC Date, Time, and Timestamp reference pages

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: