Core Java

Adding Minutes to Time in Java: Legacy Date API vs. Date Time API

In Java, handling time has evolved over the years, with the introduction of the Date Time API in Java 8, providing a more comprehensive and user-friendly approach compared to the legacy Date API. In this article, we will explore how to add minutes to a time represented as a string using both the legacy Date API and the Date Time API.

1. Adding Minutes using Date Time API

Java 8 introduced the Date Time API to overcome the limitations of the legacy Date API. The LocalTime class is specifically designed to represent time without a date component.

The code snippet below shows how to add minutes using the Date Time API:

public class DateTimeApiExample {

    public static void main(String[] args) {
        
        // Time in String format
        String timeString = "15:30:00";
        
        // minutes to add
        int minutesToAdd = 10;

        //Create a formatter with the pattern HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        
        //Create a LocalTime object
        LocalTime time = LocalTime.parse(timeString, formatter);

        // Adding minutes to time
        LocalTime newTime = time.plusMinutes(minutesToAdd);
        String newTimeString = newTime.format(formatter);

        System.out.println("Original Time: " + timeString);
        
        // Display the new Time
        System.out.println("New Time with Date Time API: " + newTimeString);
        
    }
    
}

In the above code snippet:

timeString represents the original time in the format “15:30:00”. minutesToAdd specifies the number of minutes to add to the original time (in this case, 10 minutes). DateTimeFormatter creates a formatter with the specified pattern (“HH:mm:ss”) to parse and format the time. LocalTime parses the timeString using the formatter, creating a LocalTime object. The plusMinutes() method adds the specified minutes (minutesToAdd) to the original time.

The output is:

Fig 1: output from running add minutes to time string using Date-Time API in Java
Fig 1: output from running add minutes to time string using Date-Time API in Java

1.1 Add Minutes to Current Time using Date Time API

We can use the plusMinutes() method to add minutes to the current time by calling the now() method to get the current time and then use the plusMinutes() method to add the required number of minutes.

Here’s how to add 30 minutes to the current time using Date Time API:

public class AddMinutesToCurrentTime {

    public static void main(String[] args) {
        
        // Get the current time
        LocalDateTime currentTime = LocalDateTime.now();

        // Add minutes to the current time
        int minutesToAdd = 30;
        LocalDateTime newTime = currentTime.plusMinutes(minutesToAdd);

        // Format the times for display
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String currentTimeString = currentTime.format(formatter);
        String newTimeString = newTime.format(formatter);

        // Output the results
        System.out.println("Current Time: " + currentTimeString);
        System.out.println("New Time: " + newTimeString);
    }
}

2. Adding Minutes using Legacy Date API

The legacy Date API, available before Java 8, provides the java.util.Date class for representing dates and times. However, it is known for its limitations and has been largely superseded by the more modern Date Time API.

To add minutes to a time using the legacy Date API, we can use the Calendar class:

public class LegacyDateApiExample {

    public static void main(String[] args) throws ParseException {
        
        String timeString = "9:30:00";
        int minutesToAdd = 10;

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = sdf.parse(timeString);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutesToAdd);

        Date newTime = calendar.getTime();
        String newTimeString = sdf.format(newTime);

        System.out.println("Original Time: " + timeString);
        System.out.println("New Time (Legacy Date API): " + newTimeString);
        
    }
}

In the above code

We start by creating a timeString that represents the original time in the format “9:30:00”. Next, we declared an int – minutesToAdd that specifies the number of minutes to add to the original time (in this case, 10 minutes).

We use the SimpleDateFormat to create a date formatter with the specified pattern (“HH:mm:ss”) to parse and format the time and then create a Date object that parses the timeString using the formatted.

Calendar is used to retrieve an instance of the Calendar class and sets it to the parsed date. We then use add(Calendar.MINUTE, minutesToAdd) method to add the specified minutes (minutesToAdd) to the original time using the Calendar object.

Finally, we use the calendar.getTime() to retrieve the updated time as a Date object from the Calendar and use format() method to format the new time using the same formatter.

3. Conclusion

In the context of this article, we have explored two methods for adding minutes to a time string in Java. While both the legacy Date API and the Date Time API can be used to add minutes to a time represented as a string, the Date Time API offers a more modern and concise approach. Using the Date Time API for new projects is recommended as it provides better features, immutability, and thread safety compared to the legacy Date API.

4. Download the Source Code

This was an example of Adding Minutes to Time in Java.

Download
You can download the full source code of this example here: java string time add minutes

Omozegie Aziegbe

Omos holds a Master degree in Information Engineering with Network Management from the Robert Gordon University, Aberdeen. Omos is currently a freelance web/application developer who is currently focused on developing Java enterprise applications with the Jakarta EE framework.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button