Java Date FAQ: How do I get today’s date?

Java date FAQ: Can you show me how to get today’s date in Java?

Solution: Use the java.util.Date() constructor

You can use this approach to create a new Date in Java:

Date d = new java.util.Date();

At some point I thought that approach was deprecated, but when I look at the Java 9 Date Javadoc now (April, 2018), I don’t see that it’s deprecated. So this is the simplest way to create a new Java Date instance to represent the current date and time.

Another option: Using java.util.Calendar

Because I thought that Date constructor was deprecated, at some point I began using the Java Calendar class to create a Date object that represents “today” or “now.” The following code shows how to do this:

Date date = java.util.Calendar.getInstance().getTime();

Bonus: Formatting the current Java Date output

To provide a simple human-readable format to the Java Date output, you use a SimpleDateFormat class, as shown in the following Java Date, Calendar, and SimpleDateFormat example:

import java.util.Date;
import java.text.SimpleDateFormat;

 * Returns a folder name based on the current date/time, something
 * like "20080725.013755".
public String getBackupFolderName() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.hhmmss");
    return sdf.format(date);

Hopefully that sample code doesn’t need too much explanation. In short, I create a Date object, then specify the SimpleDateFormat I want to use to format the date output, then I return that formatted output from the method as a String.

Summary: Java current date

In summary, if you need to create a new date in Java, I hope these examples are helpful.