Java Program to Calculate difference between two Time Periods


To work with date and time in java, we need to import java.time, java.util and java.text packages. We are going to use the following classes and methods provided by these packages to calculate difference between two time periods in java −

  • 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.

Approach 1: Using SimpleDateFormat and Date class

SimpleDateFormat − It is a class in java that allows us to convert date to string (formatting) and convert string to date (parsing) in local format.

Date − It is a class that signifies a certain time period (in milliseconds).

Example

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");
   } 
}

Output

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

In the above code, we have taken two time periods with the format HH:mm:ss. We have created one object named timeformat for SimpleDateFormat class. To convert text from a string to date as per the given format, we have used the parse() method.

Difference including Year and Date

Example

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

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

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

Approach 2: Using LocalDate and Period class

LocalDate − It 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 − This class is present in java.time package. It uses date based values only.

Example

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

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

In this approach, we have calculated the difference between two time periods using yyyy, MM, dd format. We created a ‘diff’ object for Period class to store differences. Using an inbuilt method ‘between()’ of Period class we have find difference between two dates. It accepts two parameters, i.e. start and end date.

When we change the Format

Example

import java.time.*;
import java.util.*;
public class Main {  
   public static void main(String[] args){
      LocalDate d1 = LocalDate.parse("2021-01-20");
      LocalDate d2 = LocalDate.parse("2023-04-01");
      Period diff = Period.between(d1, d2);
      System.out.printf("Difference between these two time periods is: " + diff);
   }
}

Output

Difference between these two time periods is: P2Y2M12D

In the above program, we have changed the format to yyyy-MM-dd.

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: 25-Apr-2023

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements