Java Program to Calculate Difference Between Two Time Periods

Created with Sketch.

Java Program: Calculate Difference Between Two Time Periods

In this blog post, we’ll explore a Java program that calculates the difference between two time periods. The program takes two time values as input, representing the start and end times, and computes the duration between them. We will provide a step-by-step explanation of the algorithm, showcase the Java code, and include examples with corresponding outputs.

Understanding the Algorithm

To calculate the difference between two time periods, we need to consider the hours, minutes, and seconds separately. Here are the key steps:

  1. Input Time Values: Receive the start and end times as input.

  2. Convert to Seconds: Convert both times to total seconds to simplify the calculation.

  3. Calculate Duration: Subtract the start time in seconds from the end time in seconds to get the total duration.

  4. Convert to Hours, Minutes, Seconds: Convert the total duration back to hours, minutes, and seconds for a more readable output.

  5. Output Result: Display the calculated duration between the two time periods.

Java Program for Calculating Time Difference

Let’s implement the algorithm in a Java program:

import java.util.Scanner;

public class TimeDifferenceCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input start time
        System.out.print("Enter the start time (HH:MM:SS): ");
        String startTimeStr = scanner.nextLine();

        // Input end time
        System.out.print("Enter the end time (HH:MM:SS): ");
        String endTimeStr = scanner.nextLine();

        // Calculate time difference
        long durationInSeconds = calculateTimeDifference(startTimeStr, endTimeStr);

        // Convert duration to hours, minutes, and seconds
        long hours = durationInSeconds / 3600;
        long minutes = (durationInSeconds % 3600) / 60;
        long seconds = durationInSeconds % 60;

        // Output result
        System.out.println("Time difference: " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds.");
    }

    // Function to calculate time difference in seconds
    private static long calculateTimeDifference(String startTime, String endTime) {
        String[] startParts = startTime.split(":");
        String[] endParts = endTime.split(":");

        // Convert times to seconds
        long startTimeInSeconds = Integer.parseInt(startParts[0]) * 3600
                + Integer.parseInt(startParts[1]) * 60
                + Integer.parseInt(startParts[2]);
        long endTimeInSeconds = Integer.parseInt(endParts[0]) * 3600
                + Integer.parseInt(endParts[1]) * 60
                + Integer.parseInt(endParts[2]);

        // Calculate time difference
        return endTimeInSeconds - startTimeInSeconds;
    }
}

Output Example

Example: Calculate Time Difference

Let’s input start time as 12:30:45 and end time as 15:45:20:

Input

Enter the start time (HH:MM:SS): 12:30:45
Enter the end time (HH:MM:SS): 15:45:20

Output

Time difference: 3 hours, 14 minutes, 35 seconds.

Explanation

The program defines a function calculateTimeDifference that takes two time values as input and calculates the time difference in seconds. It then converts this duration back to hours, minutes, and seconds for a more user-friendly output.

Conclusion

This Java program demonstrates how to calculate the time difference between two time periods. Understanding time calculations is crucial in applications where precise timing is required, such as scheduling or event management systems.

Feel free to modify and extend this program to meet your specific needs. You can experiment with different time inputs to see how the program handles various scenarios. If you have any questions or want to explore more Java programming topics, feel free to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *