Java Program to Calculate Difference Between Two Time Periods

Java Program to Calculate Difference Between Two Time Periods

What is  a Calendar class?

In this Article, we will write a Program to Calculate Difference Between Two Time Periods in Java.
The Calendar class in the Java programming language is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

Calendar Class:

The Calendar class also provides a number of methods for adding or subtracting time from a calendar, such as add() and roll(), and for comparing two calendars.

Program to Calculate Difference Between Two Time Periods

Run
// Importing all the required packages
import java.util.Scanner;
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        
        // Input for first time period
        System.out.println("Enter values for the first time period:");
        System.out.print("Hours: ");
        int hours1 = scn.nextInt();
        System.out.print("Minutes: ");
        int minutes1 = scn.nextInt();
        System.out.print("Seconds: ");
        int seconds1 = scn.nextInt();
        
        // Input for second time period
        System.out.println("Enter the second time period:");
        System.out.print("Hours: ");
        int hours2 = scn.nextInt();
        System.out.print("Minutes: ");
        int minutes2 = scn.nextInt();
        System.out.print("Seconds: ");
        int seconds2 = scn.nextInt();
        
        // Calculating total number of seconds for first period
        int totalSeconds1 = hours1 * 3600 + minutes1 * 60 + seconds1;
        
        // Calculating total number of seconds for second period
        int totalSeconds2 = hours2 * 3600 + minutes2 * 60 + seconds2;
        
        // Calculating difference between seconds of two periods
        int differenceInSeconds = Math.abs(totalSeconds1 - totalSeconds2);
        
        // Calculating difference in time periods
        int hoursDifference = differenceInSeconds / 3600;
        int minutesDifference = (differenceInSeconds % 3600) / 60;
        int secondsDifference = differenceInSeconds % 60;
        
        System.out.println("Time difference: " + hoursDifference + " hours, " + minutesDifference + " minutes, " + secondsDifference + " seconds.");
    }
}

Input:

Enter the first time period:
Hours: 12
Minutes: 34
Seconds: 55
Enter the second time period:
Hours: 8
Minutes: 12
Seconds: 15

Output:

Time difference: 4 hours, 22 minutes, 40 seconds.

Explanation:

In the above program, The user enters the first time period and then the hours, minutes, and seconds of that time period, and then the user enters the second time period and then the hours, minutes, and seconds of that time period. The program calculates the total number of seconds in each time period by multiplying the hours by 3600, the minutes by 60, and adding the seconds. The program calculates the absolute difference between the two total numbers of seconds.
The program calculates the number of hours, minutes, and seconds in the difference by dividing the difference in seconds by 3600 to get the number of hours, taking the remainder of that division and dividing by 60 to get the number of minutes, and taking the remainder of the second division to get the number of seconds. The program outputs the time difference in hours, minutes, and seconds.

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription