Java Program to Calculate difference between two Time Periods



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 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 the difference between two time periods.

Some examples where the difference between two time periods is required are finding the travel time of a flight or train between two destinations, checking data logging, creating schedules for different events, etc.

Using SimpleDateFormat and Date class

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

Example 1

In the code below, we have taken two time periods with the format HH:mm:ss. To convert text from a string to a 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 produces the following resul -

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

Example 2

In the same program, we have calculated the difference between time periods, 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");
   }
} 

The 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 a 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 the yyyy-MM-dd format. For this, we have used an inbuilt method named between() of the 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
Updated on: 2025-06-19T10:19:22+05:30

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements