Java Calendar Between getDaysBetween(Calendar d1, Calendar d2)

Here you can find the source of getDaysBetween(Calendar d1, Calendar d2)

Description

get Days Between

License

Apache License

Declaration

public static int getDaysBetween(Calendar d1, Calendar d2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

public class Main {

    public static int getDaysBetween(Calendar d1, Calendar d2) {

        int days = 0;
        int years = d1.get(Calendar.YEAR) - d2.get(Calendar.YEAR);
        if (years == 0) {
            days = d2.get(Calendar.DAY_OF_YEAR)
                    - d1.get(Calendar.DAY_OF_YEAR);
            return days;
        } else if (years > 0) {
            for (int i = 0; i < years; i++) {
                d2.add(Calendar.YEAR, 1);
                days += -d2.getActualMaximum(Calendar.DAY_OF_YEAR);
                if (d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)) {
                    days += d2.get(Calendar.DAY_OF_YEAR)
                            - d1.get(Calendar.DAY_OF_YEAR);
                    return days;
                }//w w  w  .j a  va2 s.c o m
            }
        } else {

            for (int i = 0; i < -years; i++) {
                d1.add(Calendar.YEAR, 1);
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                if (d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)) {
                    days += d2.get(Calendar.DAY_OF_YEAR)
                            - d1.get(Calendar.DAY_OF_YEAR);
                    return days;
                }
            }

        }

        return days;

    }
}

Related

  1. daysBetween(final Calendar startDate, final Calendar endDate)
  2. daysBetween(final Calendar startDate, final Calendar endDate)
  3. daysBetweenCalendarDates(final Calendar firstDate, final Calendar secondDate)
  4. daysBetweenForDate(Calendar startDate, Calendar endDate)
  5. daysBetweenxX(Calendar start, Calendar end)
  6. getDaysBetween(Calendar d1, Calendar d2)
  7. getDaysBetween(Calendar firstDate, Calendar lastDate)
  8. getDaysBetween(Calendar start, Calendar end)
  9. getDaysBetween(Calendar startDate, Calendar endDate)