Introduction

   When working with time intervals, you may encounter scenarios where you need to sum two different time intervals. This is particularly useful in scheduling systems, time tracking applications, or any situation where durations need to be combined. In this blog post, we'll explore how to create a Java program to sum two time intervals and display the result.

Understanding the Problem

    Given two time intervals in hours, minutes, and seconds, we want to calculate their sum. If the sum of seconds exceeds 60, it should be converted to minutes, and similarly, if the sum of minutes exceeds 60, it should be converted to hours.


Code

    
        
        import java.io.*;

        class time1 {
            int hr, min, sec;
        
            // Constructor to initialize the time1 object
            time1(int h, int m, int s) {
                hr = h;
                min = m;
                sec = s;
            }
        
            // Default constructor
            time1() {}
        
            // Method to add two time intervals
            time1 addtime(time1 t1, time1 t2) {
                time1 t = new time1();
        
                // Adding seconds and handling overflow to minutes
                t.sec = t1.sec + t2.sec;
                t.min = t.sec / 60;
                t.sec = t.sec % 60;
        
                // Adding minutes and handling overflow to hours
                t.min = t.min + t1.min + t2.min;
                t.hr = t.min / 60;
                t.min = t.min % 60;
        
                // Adding hours
                t.hr = t.hr + t1.hr + t2.hr;
        
                return t;
            }
        }
        
        public class tt {
            public static void main(String args[]) throws IOException {
                int h1, h2, m1, m2, s1, s2;
                DataInputStream j = new DataInputStream(System.in);
        
                System.out.println("Enter the first time (hr, min, sec):");
                h1 = Integer.parseInt(j.readLine());
                m1 = Integer.parseInt(j.readLine());
                s1 = Integer.parseInt(j.readLine());
        
                System.out.println("Enter the second time (hr, min, sec):");
                h2 = Integer.parseInt(j.readLine());
                m2 = Integer.parseInt(j.readLine());
                s2 = Integer.parseInt(j.readLine());
        
                time1 t1 = new time1(h1, m1, s1);
                time1 t2 = new time1(h2, m2, s2);
                time1 t3 = new time1();
        
                t3 = t3.addtime(t1, t2);
        
                System.out.println("Sum = " + t3.hr + ":" + t3.min + ":" + t3.sec);
            }
        }
        
        


Output


        Enter the first time(hr, min, sec)
        10
        20
        15
        Enter the second time(hr, min, sec)
        5
        23
        28
        Sum = 15:43:43
        


   

Explanation

1. Input Handling:
    The program begins by asking the user to enter the hours, minutes, and seconds for two different time intervals.
2. Adding the Time Intervals:
    The addtime method adds the seconds from both intervals. If the sum of seconds exceeds 60, it calculates the overflow into minutes.
Next, it adds the minutes and handles any overflow into hours.
Finally, it sums up the hours.
3. Displaying the Result:
    The resulting time interval, after summing the two input intervals, is displayed in the format hh:mm:ss.

Conclusion

     Summing time intervals might seem straightforward, but handling the overflow of seconds to minutes and minutes to hours requires careful attention. The Java program we've discussed provides a clear and concise way to perform this operation, making it easy to integrate into larger applications where time calculations are necessary.

With this program, you can now accurately add two time intervals and ensure that the result is properly formatted, regardless of the input values.

Happy coding!