Java Data Type How to - Get the time difference between the two dates in hh:mm format


We would like to know how to get the time difference between the two dates in hh:mm format.


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//w  w  w  .j a  va 2  s  .  com
public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MM/dd/yyyy HH:mm:ss");
        long second = 1000l;
        long minute = 60l * second;
        long hour = 60l * minute;

        // parsing input
        Date date1 = dateFormat.parse("02/26/2015 09:00:00");
        Date date2 = dateFormat.parse("02/26/2015 19:30:00");

        // calculation
        long diff = date2.getTime() - date1.getTime();

        // printing output
        System.out.print(String.format("%02d", diff / hour));
        System.out.print(String.format("%02d", (diff % hour) / minute));
        System.out.print(String.format("%02d", (diff % minute) / second));


The code above generates the following result.