Java Program to find the difference between two dates

Hello Everyone! this tutorial will focus on how to find the difference between two dates in Java. This program will help you to develop more knowledge about packages in Java.

First of all, we need to understand about java.util.* and java.text.*  which are basically packages in Java which contains a collection of classes.

Please Remember there could be multiple ways to solve a particular problem as per your understanding.




Some important classes inside these packages (which we will use) are- SimpleDateFormat, Date.

We will use the object of the SimpleDateFormat class to format the date while the object of the Date class will be used to hold the date.

How to find the difference between two dates in Java

Here is the Java code provided below

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
 
  public static void main(String[] args) {
    try {
            String d1 = "07/15/2019";
            String t1 = "11:00 AM";
            String d2 = "07/17/2019";
           String t2 = "12:15 AM";
 
           String ft = "MM/dd/yyyy hh:mm a";
 
           SimpleDateFormat smpledateformat = new SimpleDateFormat(ft);
 
           Date date1 = smpledateformat.parse(d1 + " " + t1);
           Date date2 = smpledateformat.parse(d2 + " " + t2);
           System.out.println(date1);
           System.out.println(date2 + "\n");
           // getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object
           long d = date2.getTime() - date1.getTime();
 
           int dDays = (int) (d / (24 * 60 * 60 * 1000));
           System.out.println("difference in days: " + dDays);
 
           int dhours = (int) (d / (60 * 60 * 1000));
           System.out.println("difference in hours: " + dhours);
  
           int dmin = (int) (d / (60 * 1000));
           System.out.println("difference in minutes: " + dmin);
 
           int dsec = (int) (d / (1000));
           System.out.println("difference in seconds: " + dsec);              
           System.out.println("difference in milliseconds: " + d);
 
      }
      catch (Exception e) {
           System.out.println(e);
      }
  }
}

In the above code, a few things you may notice :

  1. We called the parse() method with the help of the simpledateformat object of class SimpleDateFormat which helps to create the Date objects in the required format.
  2. We typecast the result using (int) because we want answers to be in an integer format.
  3.  We put the whole code in a try block because in case if any exception may arise during parsing can easily be caught by the catch statement.

The output of the above code is :

Mon Jul 15 11:00:00 UTC 2019
Wed Jul 17 00:15:00 UTC 2019

difference in days: 1
difference in hours: 37
difference in minutes: 2235
difference in seconds: 134100
difference in milliseconds: 134100000

So now you understood how to make a Java Program to find the difference between two dates.

You may like this tutorial too:

add some hours to the current time in Java


Leave a Reply

Your email address will not be published. Required fields are marked *