Java Calendar Different dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)

Here you can find the source of dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)

Description

date Diff

License

Open Source License

Declaration

private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) 

Method Source Code


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

import java.util.Calendar;

public class Main {
    private static final int MAX_YEARS = 100000;

    private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) {
        int year = Calendar.YEAR;

        int fromYear = fromDate.get(year);
        int toYear = toDate.get(year);
        if (Math.abs(fromYear - toYear) > MAX_YEARS) {
            toDate.set(year, fromYear + (future ? MAX_YEARS : -MAX_YEARS));
        }/*w  w w.  ja va2  s  . co  m*/

        int diff = 0;
        long savedDate = fromDate.getTimeInMillis();
        while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
            savedDate = fromDate.getTimeInMillis();
            fromDate.add(type, future ? 1 : -1);
            diff++;
        }
        diff--;
        fromDate.setTimeInMillis(savedDate);
        return diff;
    }
}

Related

  1. countDiffDay(Calendar c1, Calendar c2)
  2. countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp)
  3. datediff(Calendar c1, Calendar c2)
  4. dateDiff(int type, Calendar fromDate, Calendar toDate)
  5. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  6. dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
  7. dateDifferenceInMin(Calendar startDate, Calendar stopDate)
  8. diff(Calendar value1, Calendar value2, int millisInCondition)
  9. diffDay(Calendar calendar, int day)