Java Day End countDaysBetween(Date start, Date end)

Here you can find the source of countDaysBetween(Date start, Date end)

Description

Calculates the number of days between start and end dates, taking into consideration leap years, year boundaries etc.

License

Open Source License

Parameter

Parameter Description
start the start date
end the end date, must be later than the start date

Return

the number of days between the start and end dates

Declaration

public static long countDaysBetween(Date start, Date end) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .ja v a2 s .c om
 * Utilities.java
 *
 * Copyright (c) 2009 Jay Lawson <jaylawson39 at yahoo.com>. All rights reserved.
 *
 * This file is part of MekHQ.
 *
 * MekHQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MekHQ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MekHQ.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;

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

public class Main {
    private static final int MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24;

    /**
     * Calculates the number of days between start and end dates, taking
     * into consideration leap years, year boundaries etc.
     *
     * @param start the start date
     * @param end the end date, must be later than the start date
     * @return the number of days between the start and end dates
     */
    public static long countDaysBetween(Date start, Date end) {
        if (end.before(start)) {
            throw new IllegalArgumentException("The end date must be later than the start date");
        }

        //reset all hours mins and secs to zero on start date
        Calendar startCal = GregorianCalendar.getInstance();
        startCal.setTime(start);
        startCal.set(Calendar.HOUR_OF_DAY, 0);
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        long startTime = startCal.getTimeInMillis();

        //reset all hours mins and secs to zero on end date
        Calendar endCal = GregorianCalendar.getInstance();
        endCal.setTime(end);
        endCal.set(Calendar.HOUR_OF_DAY, 0);
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        long endTime = endCal.getTimeInMillis();

        return (endTime - startTime) / MILLISECONDS_IN_DAY;
    }
}

Related

  1. calculateMaxStartDate(Calendar calendar, int daysAhead)
  2. calcYears(Date startDate, Date endDate)
  3. calenderFromDateString(String dateAndTime)
  4. checkTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate, int skewInMinutes)
  5. checkTimePeriod(final Date beginHour, final Date endHour, final Date hour)
  6. countMonths(java.util.Date startDate, java.util.Date endDate)
  7. dateBetweens(Date start, Date end)
  8. dateSpan(Date beginDate, Date endDate)
  9. dayBetweenTwoDates(Date beginDate, Date endDate)