Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaJava Program to Calculate Difference Between Two Time Periods

Java Program to Calculate Difference Between Two Time Periods

In this article, let’s explore the various methods to find the difference between the Two Time Periods in Java. For simplicity, let’s assume that the Time Period provided to us is in the format HH:MM:SS

Example

Input : 1st Time Period :- 18:00:00
    2nd Time Period :- 21:00:00
Output: 3 hours 0 minutes and 0 seconds
 
Input : 1st Time Period :- 17:00:00
        2nd Time Period :- 23:22:00
Output: 6 hours 22 minutes and 0 seconds

 

Method 1 :- Using SimpleDateFormat class and Date class

SimpleDateFormat class has been added to the java.text package in the 7th JDK version. Parse the Time Period in the format HH:MM:SS by creating a SimpleDateFormat object. SimpleDateFormat object parses the Time period and returns a Date object which can be used to calculate the time elapsed.

Below is the code for the above approach:

Java




// Java Program to Find the difference
// between Two Time Periods
  
// Importing the Date Class from the util package
import java.util.*;
  
// Importing the SimpleDateFormat
// Class from the text package
import java.text.*;
  
public class GFG {
  
    public static void main(String[] args) throws Exception
    {
  
        // Dates to be parsed
        String time1 = "18:00:00";
        String time2 = "7:30:50";
  
        // Creating a SimpleDateFormat object
        // to parse time in the format HH:MM:SS
        SimpleDateFormat simpleDateFormat
            = new SimpleDateFormat("HH:mm:ss");
  
        // Parsing the Time Period
        Date date1 = simpleDateFormat.parse(time1);
        Date date2 = simpleDateFormat.parse(time2);
  
        // Calculating the difference in milliseconds
        long differenceInMilliSeconds
            = Math.abs(date2.getTime() - date1.getTime());
  
        // Calculating the difference in Hours
        long differenceInHours
            = (differenceInMilliSeconds / (60 * 60 * 1000))
              % 24;
  
        // Calculating the difference in Minutes
        long differenceInMinutes
            = (differenceInMilliSeconds / (60 * 1000)) % 60;
  
        // Calculating the difference in Seconds
        long differenceInSeconds
            = (differenceInMilliSeconds / 1000) % 60;
  
        // Printing the answer
        System.out.println(
            "Difference is " + differenceInHours + " hours "
            + differenceInMinutes + " minutes "
            + differenceInSeconds + " Seconds. ");
    }
}


Output

Difference is 10 hours 29 minutes 10 Seconds. 

Time Complexity: O(1)

 

Method 2 :- Using the LocalTime and ChronoUnit class

Java has brought a ton of features in the 8th JDK version and few of them are the LocalTime and ChronoUnit classes present in the java.time package. LocalTime object parses the date in the format HH:MM:SS and ChronoUnit is used to get the difference in hours, minutes and seconds.

Below is the code for the above approach:

Java




// Java program to get the difference
// between Two Time Periods in Java
  
// Importing the LocalTime class
import java.time.*;
  
// Importing the ChronoUnit class
import java.time.temporal.ChronoUnit;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Parsing Time Period in the format HH:MM:SS
        LocalTime time1 = LocalTime.of(18, 00, 00);
        LocalTime time2 = LocalTime.of(21, 22, 00);
  
        // Calculating the difference in Hours
        long hours = ChronoUnit.HOURS.between(time1, time2);
  
        // Calculating the difference in Minutes
        long minutes
            = ChronoUnit.MINUTES.between(time1, time2) % 60;
  
        // Calculating the difference in Seconds
        long seconds
            = ChronoUnit.SECONDS.between(time1, time2) % 60;
  
        // Printing the difference
        System.out.println(
            "Difference is " + hours + " hours " + minutes
            + " minutes " + seconds + " seconds.");
    }
}


Output

Difference is 3 hours 22 minutes 0 seconds.

Time Complexity: O(1)

RELATED ARTICLES

Most Popular

Recent Comments