Course – LS – All

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

>> CHECK OUT THE COURSE

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.

As always, the source code for the examples is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!