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. Introduction

Dealing with date and time values is a common task in software development, especially when building applications that involve scheduling, logging, or any time-sensitive operations. In the same context, the OffsetDateTime class in Java provides a robust solution for representing date and time information with an offset from UTC/GMT.

In this tutorial, we’ll explore how to efficiently convert a string representing date and time information into an OffsetDateTime object in Java.

2. Using OffsetDateTime.parse() Method

One of the simplest approaches to converting a string to an OffsetDateTime object is using the OffsetDateTime.parse(CharSequence text) method. This method parses the input string according to the ISO-8601 format and returns an OffsetDateTime object representing the parsed date and time:

String dateTimeString = "2024-04-11T10:15:30+01:00";
@Test
public void givenDateTimeString_whenUsingOffsetDateTimeParse_thenConvertToOffsetDateTime() {
    OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTimeString);

    OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, ZoneOffset.ofHours(1));
    assertEquals(expected, offsetDateTime);
}

In this example, the dateTimeString represents a specific date and time formatted according to the ISO-8601 standard. Moreover, we use the OffsetDateTime.parse() method, which interprets the input string dateTimeString and constructs an OffsetDateTime object accordingly.

Furthermore, this parsing process involves extracting the individual components of the date and time from the string and determining the offset from UTC/GMT. The resulting OffsetDateTime object represents the parsed date and time information, including the offset.

3. Using DateTimeFormatter With OffsetDateTime.parse()

Another approach to converting strings to OffsetDateTime objects is by using the DateTimeFormatter class to specify the input string’s format explicitly. Note that this approach provides more flexibility and allows for parsing strings with custom date and time formats.

Here’s the implementation:

@Test
public void givenDateTimeStringAndFormatter_whenUsingDateTimeFormatter_thenConvertToOffsetDateTime() {
    String customDateTimeString = "11-04-2024 10:15:30 +0100";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss Z");

    OffsetDateTime offsetDateTime = OffsetDateTime.parse(customDateTimeString, formatter);

    OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, ZoneOffset.ofHours(1));
    assertEquals(expected, offsetDateTime);
}

Here, we utilize the DateTimeFormatter class to create a custom formatter corresponding to the input string’s format. Additionally, the pattern (dd-MM-yyyy HH:mm:ss Z) specifies the expected structure of the date and time components in the input string, including the offset from UTC/GMT.

Once the formatter is created, we employ it along with the OffsetDateTime.parse() method to parse the customDateTimeString and produce an OffsetDateTime object.

4. Conclusion

In conclusion, converting strings to OffsetDateTime objects is crucial for handling date and time in Java applications.

Whether it’s parsing date-time strings from user input or external data sources, converting them into OffsetDateTime objects allows for seamless manipulation and processing of date and time data.

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