Java Date Difference dateDiff(Date date1, Date date2)

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

Description

Returns the difference in days between date1 and date2

License

Open Source License

Parameter

Parameter Description
date1 a parameter
date2 a parameter

Return

int

Declaration

public static int dateDiff(Date date1, Date date2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from  www. ja  v  a 2 s  .c o m*/
     * Returns the difference in days between date1 and date2
     * @param date1
     * @param date2
     * @return int
     */
    public static int dateDiff(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int diff = 0;

        while (cal1.before(cal2)) {
            cal1.add(Calendar.DATE, 1);
            diff++;
        }
        return diff;
    }
}

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)
  7. dateDiff(final Date date1, final Date date2)
  8. dateDiff(int category, Date date1, Date date2)