Java Date Between daysBetweenMidnight(final Date startDate, final Date endDate)

Here you can find the source of daysBetweenMidnight(final Date startDate, final Date endDate)

Description

Berechnet die Tage zwischen zwei Tagen jeweils ab Mitternacht.

License

Open Source License

Parameter

Parameter Description
startDate Start Datum
endDate End Datum

Return

long Anzahl der Tage

Declaration

public static long daysBetweenMidnight(final Date startDate, final Date endDate) 

Method Source Code

//package com.java2s;
/*/* w  ww  .j  a  v a2 s.c o  m*/
 * Copyright (C) 2012 Calenria <https://github.com/Calenria/> and contributors
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3.0 of the License, or (at your option)
 * any later version.
 *
 * This program 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 Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 */

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

public class Main {
    /**
     * Berechnet die Tage zwischen zwei Tagen jeweils ab Mitternacht.
     * 
     * @param startDate
     *            Start Datum
     * @param endDate
     *            End Datum
     * @return long Anzahl der Tage
     */
    public static long daysBetweenMidnight(final Date startDate, final Date endDate) {
        Calendar startCal = new GregorianCalendar();
        startCal.setTime(startDate);
        startCal.set(Calendar.HOUR_OF_DAY, 0);
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        startCal.set(Calendar.MILLISECOND, 0);

        Calendar endCal = new GregorianCalendar();
        endCal.setTime(endDate);
        endCal.set(Calendar.HOUR_OF_DAY, 0);
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        endCal.set(Calendar.MILLISECOND, 0);

        Calendar date = (Calendar) startCal.clone();

        long daysBetween = 0;
        while (date.before(endCal)) {
            date.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return daysBetween;
    }
}

Related

  1. daysBetween2Dates(Date d1, Date d2)
  2. daysBetween2Dates(Date startDate, Date endDate)
  3. daysBetweenCalendars(Calendar date1, Calendar date2)
  4. daysBetweenDate(Integer startDate, Integer endDate)
  5. daysBetweenDates(Date beginDate, Date endDate)
  6. getBetweenDate(Date startDate, Date endDate)
  7. getBetweenDate(String d1, String d2)
  8. getBetweenDateBuckets(Date from, Date to)
  9. getBetweenDates(Date fromDate, Date toDate )