How to Compute the Difference Between Two Dates in Java


This example shows how to compute the difference between two dates. It shows how to compute the difference between the dates in days, hours, minutes, seconds and even milliseconds.
We use the Calendar class to create two instances and set the respective dates on each of them.
In the example we set the date January 1, 2020 as the first date and March 1, 2020 as the second date.
Just to make sure we got it right (remember that the month field in Calendar and Date object are zero based, meaning that January = 0) we print them out using a SimpleDateFormat instance to make them readable.
To compute the difference in milliseconds we call the method getTimeInMillis() on the Calendar class. The millisecond-difference is then used to compute every other unit in the example.

Immediately after each calculation the result is printed out to console.


package com.javadb.examples;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main
{
    public static void main(String[] args)
    {
        Calendar c1 = Calendar.getInstance();
        c1.clear();

        Calendar c2 = Calendar.getInstance();
        c2.clear();

        // Set the date for both of the calendar instance
        c1.set(2020, 0, 1);
        c2.set(2020, 2, 1);

        // Print out the dates
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("Date 1: " + sdf.format(c1.getTime()));
        System.out.println("Date 2: " + sdf.format(c2.getTime()));

        // Get the represented date in milliseconds
        long time1 = c1.getTimeInMillis();
        long time2 = c2.getTimeInMillis();

        // Calculate difference in milliseconds
        long diff = time2 - time1;

        // Difference in seconds
        long diffSec = diff 1000;
        System.out.println("Difference in seconds " + diffSec);

        // Difference in minutes
        long diffMin = diff (60 * 1000);
        System.out.println("Difference in minutes " + diffMin);

        // Difference in hours
        long diffHours = diff (60 * 60 * 1000);
        System.out.println("Difference in hours " + diffHours);

        // Difference in days
        long diffDays = diff (24 * 60 * 60 * 1000);
        System.out.println("Difference in days " + diffDays);
    }
}

The output from the above example would be:

Date 1: 2020-01-01
Date 2: 2020-03-01
Difference in seconds 5184000
Difference in minutes 86400
Difference in hours 1440
Difference in days 60

Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: