Java Date Difference dateDiff(Date date1, Date date2)

Here you can find the source of dateDiff(Date date1, Date date2)

Description

date Diff

License

Open Source License

Declaration

public static String dateDiff(Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static String dateDiff(Date date1, Date date2) {
        long milliseconds1 = date1.getTime();
        long milliseconds2 = date2.getTime();
        long diff = milliseconds2 - milliseconds1;
        long diffSeconds = diff / 1000;
        long diffMinutes = diff / (60 * 1000);
        long diffHours = diff / (60 * 60 * 1000);
        long diffDays = diff / (24 * 60 * 60 * 1000);

        diffSeconds = diffSeconds - (diffMinutes * 60);
        diffMinutes = diffMinutes - (diffHours * 60);
        diffHours = diffHours - (diffDays * 24);

        return ((diffHours < 10 ? "0" : "") + diffHours + ":" + (diffMinutes < 10 ? "0" : "") + diffMinutes + ":"
                + (diffSeconds < 10 ? "0" : "") + diffSeconds);
    }// w w w  .  j av  a 2s . c om
}

Related

  1. dateDiff(Date beginDate, Date endDate)
  2. dateDiff(Date d1, Date d2, int field)
  3. dateDiff(Date date1, Date date2)
  4. dateDiff(Date date1, Date date2)
  5. dateDiff(Date fromDate, Date toDate)
  6. DateDiff(Date startDate, Date endDate)