Java date/time FAQ: How do I get the current date (i.e., now or today) in Java? Also how do I get the current time in Java?
Solution
With Java 8 and newer — i.e., Java 11, 14, 17, etc. — use any of the follow “now” methods on these Java classes to get the current data and time:
import java.time.*;
System.out.println( LocalDate.now() ); // 2024-04-14
System.out.println( LocalTime.now() ); // 20:10:26.039386576
System.out.println( LocalDateTime.now() ); // 2024-04-14T20:10:26.110252626
System.out.println( Instant.now() ); // 2024-04-14T20:10:26.177054113Z
System.out.println( ZonedDateTime.now() ); // 2024-04-14T20:10:26.294130941Z[UTC]
As shown in the example output, if you’re interested in the current date, LocalDate.now()
is probably what you’re looking for, and if you’re interested in the current time in Java, several of those other answers may be what you’re looking for,
If you’re interested in more details about these Java “now” solutions, I write more about these in this Scala current date and time tutorial. Although those examples are in Scala, that’s only because Scala is able to use these Java classes, so what I wrote there will work just fine with Java.
I may add more to this solution over time, but for now, I hope those answers will help you.
Historical solutions
If for some reason you need to use a solution that we used in the days before Java 8, read below for those old approaches. (Again, these are much older techniques.)
Old Java “current date” solutions
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: