Java Calendar Between daysBetween(Calendar early, Calendar late)

Here you can find the source of daysBetween(Calendar early, Calendar late)

Description

days Between

License

Open Source License

Declaration

public static final int daysBetween(Calendar early, Calendar late) 

Method Source Code

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

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

public class Main {

    public static final int daysBetween(Calendar early, Calendar late) {
        return (int) (toJulian(late) - toJulian(early));
    }//from   w w  w.  j a  v a2s  .  co  m

    public static final int daysBetween(Date early, Date late) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(early);
        c2.setTime(late);
        return daysBetween(c1, c2);
    }

    /**
     * Return a Julian date based on the input parameter. This is based from
     * calculations found at
     * 
     * <pre>
     * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day
     * Calculations (Gregorian Calendar)</a>, provided by Bill Jeffrys.
     * </pre>
     * 
     * @param c
     *            a calendar instance
     * @return the julian day number
     */
    public static final float toJulian(Calendar c) {
        int Y = c.get(Calendar.YEAR);
        int M = c.get(Calendar.MONTH);
        int D = c.get(Calendar.DATE);
        int A = Y / 100;
        int B = A / 4;
        int C = 2 - A + B;
        float E = (int) (365.25f * (Y + 4716));
        float F = (int) (30.6001f * (M + 1));
        float JD = C + D + E + F - 1524.5f;

        return JD;
    }
}

Related

  1. daysBetween(Calendar early, Calendar late)
  2. daysBetween(Calendar fromCalendar, Calendar untilCalendar)
  3. daysBetween(Calendar fromDate, Calendar toDate)
  4. daysBetween(Calendar start, Calendar end)