Java Date Difference diff(Date date1, Date date2)

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

Description

diff

License

Open Source License

Declaration

public static int diff(Date date1, Date date2) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static int diff(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(date1);//w  w  w .jav a  2s.  c  o  m
        c2.setTime(date2);
        int diffDay = 0;

        if (c1.before(c2)) {
            diffDay = countDiffDay(c1, c2);
        } else {
            diffDay = countDiffDay(c2, c1);
        }

        return diffDay;
    }

    public static int countDiffDay(Calendar c1, Calendar c2) {
        int returnInt = 0;
        while (!c1.after(c2)) {
            c1.add(Calendar.DAY_OF_MONTH, 1);
            returnInt++;
        }

        if (returnInt > 0) {
            returnInt = returnInt - 1;
        }

        return (returnInt);
    }
}

Related

  1. dayDiff(Date date1, Date date2)
  2. dayDiff(Date first, Date second)
  3. dayDiffByStartOfDay(Date a, Date b)
  4. daysDiff(Date d1, Date d2)
  5. diff(Date d1, Date d2)
  6. diff(Date date1, Date date2)
  7. diff(Date endDate, Date startDate)
  8. diff(Date sDate, Date fDate)
  9. diff(Date subtrahend, Date minuend, long diffField)