Calculate Difference Between Two Time Periods in Java



We are given two time periods and our task is to write a Java program to calculate the difference between them. For this problem, we can use classes and methods of the java.time, java.util and java.text packages.

  • SimpleDateFormat class
  • Date class
  • LocalDate class
  • Period class
  • parse() method
  • between() method

As we move further in this article, we will understand the uses of these classes and methods in calculating difference between two time periods.

Using SimpleDateFormat and Date class

SimpleDateFormat is a class in Java that allows us to convert date to string (formatting) and convert string to date (parsing) in local format. The Date is a Java class that signifies a certain time period (in milliseconds).

Example 1

In the below code, we have taken two time periods with the format HH:mm:ss. To convert text from a string to date as per the given format, we have used the parse() method.

import java.text.*;
import java.util.Date;
public class Main {
   public static void main(String[] args) throws Exception {
      String timePeriod1 = "09:00:00";
      String timePeriod2 = "10:20:00";
      SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
      Date dat1 = timeformat.parse(timePeriod1);
      Date dat2 = timeformat.parse(timePeriod2);
      long diffInMs = dat2.getTime() - dat1.getTime();
      long diffInSec = diffInMs / 1000 % 60;
      long diffInMin = diffInMs / (60 * 1000) % 60;
      long diffInHr = diffInMs / (60 * 60 * 1000) % 24;
      System.out.format("Difference between these two time periods is: " + diffInHr +" hours " +  diffInMin +" minutes " + diffInSec + " seconds");
   } 
}

On running, this code produce following result ?

Difference between these two time periods is: 1 hours 20 minutes 0 seconds

Example 2

In the same program, we have calculated difference between time period including year and date.

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
   public static void main(String[] args) throws Exception {
      String timePeriod1 = "23/02/23 09:25:00";
      String timePeriod2 = "24/03/23 09:00:00";
      SimpleDateFormat timeformat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
      Date dat1 = timeformat.parse(timePeriod1);
      Date dat2 = timeformat.parse(timePeriod2);
      long diffInMs = dat2.getTime() - dat1.getTime();
      long diffInSec = diffInMs / 1000 % 60;
      long diffInMin = diffInMs / (60 * 1000) % 60;
      long diffInHr = diffInMs / (60 * 60 * 1000) % 24;
      long diffInDays = diffInMs / (60 * 60 * 24 * 1000) % 365;
      long diffInYears = (diffInMs / (1000l*60*60*24*365)); 
      System.out.format("Difference between these two time periods is: " +  diffInYears + " Years " + diffInDays + " Days " + diffInHr + " hours " + diffInMin + " minutes " + diffInSec + " seconds");
   }
} 

Output of the above code is as follows ?

Difference between these two time periods is: 1 Years 28 Days 23 hours 35 minutes 0 seconds

Using LocalDate and Period class

LocalDate is an immutable date-time object used to represent Date. Its default format is yyyy-MM-dd. It can't be used to store time based values, it only describes dates.

Period class is present in java.time package. It uses date based values only.

Example

In this Java program, we have calculated the difference between two time periods using yyyy-MM-dd format. For this, we have used an inbuilt method named between() of Period class.

import java.time.*;
import java.util.*;
public class Main {
   public static void main(String[] args){
      LocalDate d1 = LocalDate.of(2023, 11, 20);
      LocalDate d2 = LocalDate.of(2022, 02, 01);
      Period diff = Period.between(d2, d1);
      System.out.printf("Difference between these two time periods is: " +    diff.getYears() +" years " + diff.getMonths() + " months " + diff.getDays() + " days");
   }
}

Output obtained as ?

Difference between these two time periods is: 1 years 9 months 19 days

Conclusion

We have seen the uses of SimpleDateFormat, Date, LocalDate and Period classes of java and also understood how to use the parse() and between() method which are very important and useful while working with date and time in java programming language.

Updated on: 2024-08-01T11:01:09+05:30

746 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements