Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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:

TimeZone.setDefault(TimeZone.getTimeZone("CET"));
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)
  .format(date);

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
  .atZone(ZoneId.of("CET"))
  .toLocalDateTime();

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW