Java Calendar Between daysBetween(Calendar fromCalendar, Calendar untilCalendar)

Here you can find the source of daysBetween(Calendar fromCalendar, Calendar untilCalendar)

Description

days Between

License

Open Source License

Declaration

public static int daysBetween(Calendar fromCalendar, Calendar untilCalendar) 

Method Source Code


//package com.java2s;
/*/*  w  ww .  j a  va 2  s  .co  m*/
 * (c) Copyright 2007-2013 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

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

import java.util.TimeZone;

public class Main {
    public static int daysBetween(Date from, Date until) {
        Calendar fromCalendar = calendar(from);
        Calendar untilCalendar = calendar(until);
        return daysBetween(fromCalendar, untilCalendar);
    }

    public static int daysBetween(Calendar fromCalendar, Calendar untilCalendar) {
        return julianDay(untilCalendar.get(Calendar.YEAR), untilCalendar.get(Calendar.MONTH) + 1,
                untilCalendar.get(Calendar.DAY_OF_MONTH))
                - julianDay(fromCalendar.get(Calendar.YEAR), fromCalendar.get(Calendar.MONTH) + 1,
                        fromCalendar.get(Calendar.DAY_OF_MONTH));
    }

    public static Calendar calendar(int year, int month, int day) {
        return new GregorianCalendar(year, month, day);
    }

    public static Calendar calendar(int year, int month, int day, TimeZone timeZone) {
        GregorianCalendar calendar = new GregorianCalendar(timeZone);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar;
    }

    public static Calendar calendar(int year, int month, int day, int hours, int minutes, int seconds,
            int milliseconds) {
        GregorianCalendar calendar = new GregorianCalendar(year, month, day, hours, minutes, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);
        return calendar;
    }

    public static Calendar calendar(int year, int month, int day, int hours, int minutes, int seconds,
            int milliseconds, TimeZone timeZone) {
        GregorianCalendar calendar = new GregorianCalendar(year, month, day, hours, minutes, seconds);
        calendar.setTimeZone(timeZone);
        calendar.set(Calendar.MILLISECOND, milliseconds);
        return calendar;
    }

    public static Calendar calendar(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar;
    }

    public static Calendar calendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /** Calculates the julian day of a {@link Date}. 
      *  See http://en.wikipedia.org/wiki/Julian_day */
    public static int julianDay(Date date) {
        return julianDay(calendar(date));
    }

    /** Calculates the julian day of a {@link Calendar}. 
      *  See http://en.wikipedia.org/wiki/Julian_day */
    public static int julianDay(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return julianDay(year, month + 1, day);
    }

    /** Calculates the julian day of a date. 
      *  See http://en.wikipedia.org/wiki/Julian_day */
    public static int julianDay(int year, int month, int day) {
        int a = (14 - month) / 12;
        int y = year + 4800 - a;
        int m = month + 12 * a - 3;
        int jdn = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
        return jdn;
    }
}

Related

  1. daysBetween(Calendar early, Calendar late)
  2. daysBetween(Calendar early, Calendar late)
  3. daysBetween(Calendar fromDate, Calendar toDate)
  4. daysBetween(Calendar start, Calendar end)
  5. daysBetween(Calendar startTime, Calendar endTime)
  6. daysBetween(final Calendar cal1, final Calendar cal2)