eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

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

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – Diagrid – NPI EA (cat= Testing)
announcement - icon

In distributed systems, managing multi-step processes (e.g., validating a driver, calculating fares, notifying users) can be difficult. We need to manage state, scattered retry logic, and maintain context when services fail.

Dapr Workflows solves this via Durable Execution which includes automatic state persistence, replaying workflows after failures and built-in resilience through retries, timeouts and error handling.

In this tutorial, we'll see how to orchestrate a multi-step flow for a ride-hailing application by integrating Dapr Workflows and Spring Boot:

>> Dapr Workflows With PubSub

 1. Overview

Handling date and time in a standardized format is crucial when working with applications that deal with different time zones or when exchanging data between systems.

In this tutorial, we’ll explore various techniques for formatting a LocalDate to the ISO 8601 format. This format includes the ‘T‘ separator and the ‘Z‘ indicating UTC time.

2. LocalDate and ISO 8601

LocalDate is a part of the modern date and time API introduced in Java 8 under the java.time package. It’s immutable, meaning that once an instance is created, its value cannot be changed. It represents a date without considering the time or time zone, focusing solely on the year, month, and day of the month. LocalDate facilitates convenient manipulation and interaction with date information.

ISO 8601 is an international standard for representing dates and times in a clear, unambiguous, and universally accepted format. It provides a standardized way to express dates and times, which is essential for a wide range of applications. This includes data interchange, international communication, and computer systems.

The ISO 8601 format includes several components, with the most common format being: YYYY-MM-DDThh:mm:ss.sssZ.

Here’s a breakdown of the components:

  • YYYY: Represents the year with four digits (e.g., 2023)
  • MM: Represents the month with two digits (e.g., 03 for March)
  • DD: Represents the day of the month with two digits (e.g., 15)
  • T‘: A literal ‘T’ character that separates the date from the time
  • hh: Represents the hour of the day in 24-hour format (e.g., 14 for 2 PM)
  • mm: Represents the minutes (e.g., 30)
  • ss: Represents the seconds (e.g., 45)
  • sss: Represents milliseconds (optional and may vary in length)
  • Z‘: A literal ‘Z’ character that indicates the time is in Coordinated Universal Time (UTC)

ISO 8601 allows for various optional components, making it a versatile standard for representing date and time information. For example, we can include time zone offsets or omit seconds and milliseconds when they aren’t relevant.

The ‘Z’ character indicates that the time is in UTC, but we can also represent time in local time zones by specifying the offset from UTC.

3. Using Java 8 Time API

Java provides a flexible way to format date and time objects, including LocalDate using the DateTimeFormatter class.

Instances of DateTimeFormatter are thread-safe, making them suitable for use in multi-threaded environments without the need for external synchronization.

Here’s how we can use it to format a LocalDate to ISO 8601:

class LocalDateToISO {
    String formatUsingDateTimeFormatter(LocalDate localDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        String formattedDate = localDate.atStartOfDay().atOffset(ZoneOffset.UTC).format(formatter);
        return formattedDate;
    }

In this example, we create a DateTimeFormatter with a custom pattern that includes ‘T‘ and ‘Z’ in the desired positions. Then, we use the format() method to format the LocalDate into a string with the specified format.

We can perform a test to verify its expected behavior:

@Test
void givenLocalDate_whenUsingDateTimeFormatter_thenISOFormat(){
    LocalDateToISO localDateToISO = new LocalDateToISO();
    LocalDate localDate = LocalDate.of(2023, 11, 6);

    String expected = "2023-11-06T00:00:00.000Z";
    String actual = localDateToISO.formatUsingDateTimeFormatter(localDate);
    assertEquals(expected, actual);
}

4. Using SimpleDateFormat

The SimpleDateFormat class is a powerful tool for formatting and parsing dates. It belongs to the java.text package and provides a straightforward way to convert dates between their textual representations and Date objects.

It’s particularly useful for working with legacy date-time types, such as java.util.Date. While it’s not as modern or robust as the java.time API, it can still serve this purpose:

String formatUsingSimpleDateFormat(LocalDate date) {
    Date utilDate = Date.from(date.atStartOfDay(ZoneOffset.UTC).toInstant());
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    String formattedDate = dateFormat.format(utilDate);
    return formattedDate;
}

In the above example, we’re converting a LocalDate to a ZonedDateTime with ZoneOffset.UTC and then converts it to an Instant object. We can then obtain a Date object from an Instant and perform formatting on the object.

Let’s format a LocalDate object using SimpleDateFormat:

@Test
void givenLocalDate_whenUsingSimpleDateFormat_thenISOFormat(){
    LocalDateToISO localDateToISO = new LocalDateToISO();
    LocalDate localDate = LocalDate.of(2023, 11, 6);

    String expected = "2023-11-06T00:00:00.000Z";
    String actual = localDateToISO.formatUsingSimpleDateFormat(localDate);
    assertEquals(expected, actual);
}

It’s crucial to be aware that SimpleDateFormat isn’t thread-safe. Concurrent usage by multiple threads can lead to unexpected results or exceptions. To address this concern, developers often use ThreadLocal ensuring that each thread possesses its dedicated instance of SimpleDateFormat. This helps in effectively preventing potential thread-safety issues.

5. Using Apache Commons Lang3

The Apache Commons Lang3 library provides a utility class named FastDateFormat that simplifies date formatting. It’s a fast and thread-safe version of SimpleDateFormat. We can directly substitute this class for SimpleDateFormat in the majority of the formatting and parsing scenarios. It proves particularly beneficial in multi-threaded server environments.

This approach emphasizes conciseness by utilizing the features of Apache Commons Lang 3 to create Java date formatting code that is clear and straightforward to understand.

We can easily obtain the library from the central Maven repository by including the following dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.14.0</version>
</dependency>

After installing the library, we can employ its methods. Here’s an example illustrating how to use it:

String formatUsingApacheCommonsLang(LocalDate localDate) {
    Date date = Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.UTC));
    String formattedDate = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", TimeZone.getTimeZone("UTC"))
      .format(date);
    return formattedDate;
}

The above code example takes a LocalDate, converts it to a Date, and then formats it into a string with a specific pattern using FastDateFormat to format the LocalDate to ISO 8601.

Let’s move on to testing this example:

@Test
void givenLocalDate_whenUsingApacheCommonsLang_thenISOFormat() {
    LocalDateToISO localDateToISO = new LocalDateToISO();
    LocalDate localDate = LocalDate.of(2023, 11, 6);

    String expected = "2023-11-06T00:00:00.000Z";
    String actual = localDateToISO.formatUsingApacheCommonsLang(localDate);
    assertEquals(expected, actual);
}

6. Using Joda-Time

Joda-Time is a widely-used Java library designed to address the shortcomings of the original date and time classes in java.util package. Before the advent of the java.time API in Java 8, Joda-Time served as a popular and powerful alternative for handling date and time operations.

To incorporate the features of the Joda-Time library, we should include the following dependency to our pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

While it’s no longer necessary in Java 8 and later, it remains an option for pre-existing codebases:

String formatUsingJodaTime(org.joda.time.LocalDate localDate) {
    org.joda.time.format.DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    return formatter.print(localDate.toDateTimeAtStartOfDay(DateTimeZone.UTC));
}

In the above example, the DateTimeFormatter from Joda-Time is used to format the LocalDate to ISO 8601.

Let’s test it out:

@Test
void givenLocalDate_whenUsingJodaTime_thenISOFormat() {
    LocalDateToISO localDateToISO = new LocalDateToISO();
    org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 11, 6);

    String expected = "2023-11-06T00:00:00.000Z";
    String actual = localDateToISO.formatUsingJodaTime(localDate);
    assertEquals(expected, actual);
}

7. Conclusion

In this article, we talked about the different ways of formatting a LocalDate to ISO 8601 with ‘T‘ and ‘Z‘ in Java. The choice of method depends on our preference for code readability and maintainability.

We can choose the method that best suits our needs, and ensure that our date and time representations adhere to the ISO 8601 standard for consistency and interoperability. The DateTimeFormatter approach is more flexible and suitable for handling various formatting requirements, while the other methods provide simpler solutions for specific scenarios.

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

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

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)