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

Java provides a standard API to work with date and time. The Date Time API provides a method to parse a time in the string format to an equivalent LocalTime type for further manipulation.

In this tutorial, we’ll explore how to add minutes to a time in the string format using the legacy Date API and the Date Time API.

2. Using the Legacy Date API

A time string shows time but with a string data type. Performing arithmetic operations with string is not feasible. Therefore, we need to parse a time string to an equivalent Date type before performing arithmetic operations.

The legacy Date API can parse a String time to Date. Let’s see an example that adds minutes using the legacy Date API:

@Test
void givenTimeStringUsingSimpleDateFormat_whenIncrementedWith10Minutes_thenResultShouldBeCorrect() throws ParseException {
    String timeString = "23:45";
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    Date date = timeFormat.parse(timeString);
        
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.MINUTE, 10);
    String result = timeFormat.format(cal.getTime());
    assertEquals("23:55", result);
}

In the above code, we create a String variable with a time value. Then, we create the SimpleDateFormat object to format the time to the hour and minute representation.

Next, we create a Date object and invoke parse() on timeFormat to convert the String time to a Date object for further manipulation.

Additionally, we create a Calendar object and invoke the setTime() method on it. This method accepts the Date object we created earlier. Next, we invoke the add() method on the Calendar object to add 10 minutes to the time.

Finally, we assert that the new time is equal to the expected time.

3. Using the Date Time API

The Date Time API can easily parse a time string to LocalTime. Also, it provides methods to make arithmetic operations with time easy.

Here’s some example code that uses the Date Time API to add minutes to a time string:

@Test
void givenTimeStringUsingLocalTime_whenIncrementedWith10Minutes_thenResultShouldBeCorrect() {
    String timeString = "23:45";
    LocalTime time = LocalTime.parse(timeString);
    LocalTime newTime = time.plusMinutes(10);
    String result = newTime.toString();
    assertEquals("23:55", result);
}

Here, we create a time string and pass it to a LocalTime object. The LocalTime class provides various methods to perform arithmetic operations on time. Next, we invoke the plusMinutes() method on time to add 10 minutes to it.

Finally, we assert that the new time is equal to the expected time.

4. Conclusion

In this article, we learned two ways to add minutes to a time string. Compared to the legacy Date API, the Date Time API makes it easy to manipulate time and perform arithmetic operations.

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