Here you can find the source of daysBetweenMidnight(final Date startDate, final Date endDate)
Parameter | Description |
---|---|
startDate | Start Datum |
endDate | End Datum |
public static long daysBetweenMidnight(final Date startDate, final Date endDate)
//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; } }