Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

In this tutorial, we’ll show how we can convert Date objects to String objects in Java. To do so, we’ll work with the older java.util.Date type as well as with the new Date/Time API introduced in Java 8.

If you’d like to learn how to do the opposite conversion, i.e., from String to Date types, you can check out this tutorial here.

For more details regarding new Date/Time API, please see this related tutorial.

2. Converting java.util.Date to String

Although we shouldn’t use java.util.Date if we’re working with Java 8, sometimes we have no choice (e.g., we’re receiving the Date object from a library that isn’t in our control).

In such cases, we have several ways to convert java.util.Date to String at our disposal.

2.1. Preparing the Date Object

Let’s first declare an expected String representation of our date and define a pattern of desired date format:

private static final String EXPECTED_STRING_DATE = "Aug 1, 2018 12:00 PM";
private static final String DATE_FORMAT = "MMM d, yyyy HH:mm a";

Now we need actual Date object that we’d like to convert. We’ll use a Calendar instance to create it:

Calendar calendar = Calendar.getInstance();
calendar.set(2018, Calendar.AUGUST, 1, 12, 0);
Date date = calendar.getTime();

We’ve set default TimeZone to CET to prevent issues when working with the new API later. We should note that the Date itself doesn’t have any time zone, but its toString() uses the current default time zone.

We’ll be using this Date instance in all of our examples below.

2.2. Using the SimpleDateFormat Class

We’ll make use of the format() method of the SimpleDateFormat class in this example. Let’s create an instance of it by using our date format:

DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

After this, we can format our date and compare it with the expected output:

String formattedDate = formatter.format(date);

assertEquals(EXPECTED_STRING_DATE, formattedDate);

2.3. Using the Abstract DateFormat Class

As we could’ve seen, SimpleDateFormat is a subclass of the abstract DateFormat class. This class provides various methods for date and time formatting.

We’ll use it to achieve the same output as above:

String formattedDate = DateFormat
  .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)

With this approach, we are passing style patterns — MEDIUM for the date and SHORT for the time in our case.

3. Using the Formatter Class

Another simple way of getting the same String as in earlier examples is to use the Formatter class.

While this may not be the most readable solution, it is a thread-safe one-liner that could be useful, especially in a multi-threaded environment (we should keep in mind that SimpleDateFormat is not thread-safe):

String formattedDate = String.format("%1$tb %1$te, %1$tY %1$tI:%1$tM %1$Tp", date);

We used 1$ to indicate that we’ll be passing only one argument to be used with every flag. A detailed explanation of the flags could be found on Date/Time Conversions part of the Formatter class.

4. Converting Using Java 8 Date/Time API

The Date/Time API from Java 8 is far more powerful than the java.util.Date and java.util.Calendar classes, and we should use it whenever possible. Let’s see how we can put it to use to convert our existing Date object to String.

This time, we’ll use the DateTimeFormatter class and its format() method, as well as the same date pattern, declared in Section 2.1:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);

To use the new API, we need to convert our Date object to an Instant object:

Instant instant = date.toInstant();

Since our expected String has both date and time parts, we also need to convert the Instant object to LocalDateTime:

LocalDateTime ldt = instant

And finally, we can easily get our formatted String:

String formattedDate = ldt.format(formatter);

5. Conclusion

In this article, we illustrated several ways of converting java.util.Date objects to String. We first showed how to do that using the older java.util.Date and java.util.Calendar classes and corresponding date formatting classes.

Then we used the Formatter class and, finally, the Java 8 Date/Time API.

As always, complete source code can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!