One of the most common tasks while developing an application in Java is to get the current date and time for timestamping different events. For example, when you buy a product online, send an email, or even post an update on your social media profile, you would often notice the exact creation timestamp.

In this article, we will look at different ways to get the date and time in Java using both legacy classes (Date & Calendar) as well as Java 8 new date and time API.

Java 8 Date & Time API

Java 8 introduced a whole new date and time API (classes in java.time.* package) to address the shortcomings of the existing API (java.util.Date & java.util.Calendar). The new API is not only thread-safe but also much more user-friendly with a lot of utility methods for performing different date and time tasks.

Let us look at some of the important classes from Java 8 new date and time API that you can use to get the current date and time.

LocalDate Class

As the name suggests, the LocalDate class stores date in the ISO-8601 format (yyyy-MM-dd) without any time or timezone information. This means that you can only get the current date in the system default timezone without time.

Here is an example that shows how you can use LocalDate to get the current date:

// get current date
LocalDate now = LocalDate.now();

// print date
System.out.println("Current Date: " + now);

// print date in different format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy");
System.out.println("Current Formatted Date: " + now.format(formatter));

The above code outputs the following:

Current Date: 2019-12-21
Current Formatted Date: Saturday, December 21, 2019

LocalTime Class

The LocalTime class does the opposite of LocalDate. It stores the local time in ISO 8601 format without any date or timezone information. This means that you can get the current time of the day without the actual date as shown below:

// get current time
LocalTime now = LocalTime.now();

// print time
System.out.println("Current Time: " + now);

// print time in different format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
System.out.println("Current Formatted Time: " + now.format(formatter));

You should see the following output if you run the above code snippet:

Current Time: 03:30:08.116
Current Formatted Time: 03:30 AM

LocalDateTime Class

The LocalDateTime class, the most popular date and time class in Java, holds both local date and time without any timezone information. Here is an example that demonstrates how you can use LocalDateTime to get the current date and time in Java 8 and higher:

// get current date and time
LocalDateTime now = LocalDateTime.now();

// print date and time
System.out.println("Current Date & Time: " + now);

// print date and time in different format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy HH:mm:ss");
System.out.println("Current Formatted Date & Time: " + now.format(formatter));

Here is how the output of the above code looks like:

Current Date & Time: 2019-12-21T03:36:47.324
Current Formatted Date & Time: Saturday, December 21, 2019 03:36:47

ZonedDateTime Class

Finally, ZonedDateTime is used to store both date and time along with the timezone information. Here is an example that shows how you can get the current zoned date and time using the system default timezone:

// get current zoned date and time
ZonedDateTime now = ZonedDateTime.now();

// print zoned date and time
System.out.println("Current Zoned Date & Time: " + now);

// print zoned date and time in different format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy HH:mm:ss Z");
System.out.println("Current Formatted Date & Time: " + now.format(formatter));

If you run the above code snippet, you should see something like below printed on the console:

Current Zoned Date & Time: 2019-12-21T03:42:25.688+05:00[Asia/Karachi]
Current Formatted Date & Time: Saturday, December 21, 2019 03:42:25 +0500

To get the current date and time for a different timezone, you can use ZoneId identifier as shown below:

// get current Paris date and time
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Paris"));

// print zoned date and time
System.out.println("Current Paris Date & Time: " + now);

Here is the output:

Current Paris Date & Time: 2019-12-20T23:46:26.220+01:00[Europe/Paris]

Instant Class

The Instant class is used to represent a specific moment on the timeline. You can use this class to get the current UTC date and time as EPOCH seconds or milliseconds as shown below:

// get current instance
Instant now = Instant.now();

// print current instant
System.out.println("Current Instant: " + now);

// epoch seconds/millis
System.out.println("EPOCH Seconds: " + now.getEpochSecond());
System.out.println("EPOCH Milliseconds: " + now.toEpochMilli());

The above code snippet generates the following output:

Current Instant: 2019-12-20T22:52:00.870Z
EPOCH Seconds: 1576882320
EPOCH Milliseconds: 1576882320870

Note: Check out Introduction to Java 8 Date and Time API tutorial for more new date and time API examples.

Legacy Date and Calendar API

Another way of getting the current date and time in Java is by using the legacy Date and Calendar classes. All you need to do is create an instance of Date, use SimpleDateFormat to create the desired format, and then pass the date object to SimpleDateFormat.format() method to get the current date and time as a string.

Date Class

To get the current date and time, you only need to instantiate the java.util.Date object. Optionally, if you want to display the current date and time in a different format, you can use the SimpleDateFormat class to format the Date object as shown below:

// get current date and time
Date now = new Date();

// print date object
System.out.println("Current Date & Time: " + now);

// print date object in a specific format
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm");
System.out.println("Current Formatted Date & Time: " + format.format(now));

The above code will print the console on the console:

Current Date & Time: Sat Dec 21 11:33:44 PKT 2019
Current Formatted Date & Time: Saturday, December 21, 2019 11:33

Calendar Class

The Calendar class in Java is used to convert for converting between a specific instant in time and a set of Calendar fields. To use Calendar to get the current date and time, you need to do the following:

  • Create an instance of Calendar by calling getInstance() static method of it.
  • Use Calendar.getTime() method to get the current date and time as a Date object.
  • Optionally, format the date using SimpleDateFormat to display it in a different format.

Here is an example:

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

// get current date and time
Date now = calendar.getTime();

// print date object
System.out.println("Current Date & Time: " + now);

// print date object in a specific format
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm");
System.out.println("Current Formatted Date & Time: " + format.format(now));

As you can see above, using the Calendar class is as simple as using Date. You just create a Calendar instance and then get the current date and time as a Date object. The rest of the code is similar to what we did in the previous example.

Timezone Information

By default, both Date and Calendar classes return the current date and time in the default system timezone. To get the current date and time in a different timezone, you need to explicitly set the desired timezone.

Here is an example that shows how you can set the timezone while formatting the Date object using SimpleDateFormat:

// get current date and time
Date now = new Date();

// format date and time in Europe/Paris timezone
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm z");
format.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
System.out.println("Current Paris Date & Time: " + format.format(now));

The above code will output the following:

Current Paris Date & Time: Saturday, December 21, 2019 07:57 CET

If you are using Calendar, you can use the Calendar.setTimeZone() method to change the default timezone to the one you want to as shown below:

// create calendar instance
Calendar c = Calendar.getInstance();

// set Europe/Paris timezone
c.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));

// format date and time in specific timezone
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a z");
format.setTimeZone(c.getTimeZone());
System.out.println("Current Paris Date & Time: " + format.format(c.getTime()));

Here is how the output looks like now:

Current Paris Date & Time: Saturday, December 21, 2019 08:04 AM CET

System.currentTimeMillis() Method

If you only want to get the current date and time as number of milliseconds passed since the UNIX EPOCH, just use System.currentTimeMillis(). This method returns the current time in milliseconds:

// get EPOCH milliseconds
long millis = System.currentTimeMillis();

// print milliseconds
System.out.println("EPOCH milliseconds: " + millis);

The above code snippet will print the following on the console:

EPOCH milliseconds: 1576915724838

To convert the above milliseconds into human-readable format, you can do the following:

// convert EPOCH milliseconds to date
Date date = new Date(System.currentTimeMillis());

// print date in human-reable format
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' HH:mm:ss z");
System.out.println("Current Date & Time: " + format.format(date));

Here is how the output looks like now:

Current Date & Time: Saturday, December 21, 2019 at 13:15:23 PKT

Summary

There are many scenarios where you need the current date and time in Java. In this article, we have discussed almost all possible ways to get the current date and time in Java including Java 8 new date and time API, legacy Date and Calendar classes, and more.

The new date and time API provides an extensive set of classes that has simplified working with date and time in Java 8 and higher. These classes are thread-safe, easier-to-understand, and backward compatible.

If you are working on a legacy application that uses the old Date and Calendar API, you can easily convert the legacy code to the new date and time API.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, Buy me a coffee (cost $5) .

Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.