Here you can find the source of getDaysBetween(Date start, Date end)
start
and end
.
public static int getDaysBetween(Date start, Date end)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-2015 BSI Business Systems Integration AG. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww . j a va2 s.c o m*/ * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.util.Calendar; import java.util.Date; public class Main { public static final long DAY_MILLIS = 24L * 3600L * 1000L; /** * Calculates the number of days between <code>start</code> and <code>end</code>. If the end date is before the start * date, the result will be positive as well. Example: * <ul> * <li>start = 1.1.2000, end = 2.1.2000 --> getDaysBetween(start, end) = 1 * <li>start = 2.1.2000, end = 1.1.2000 --> getDaysBetween(start, end) = 1 * </ul> * returns <code>-1</code> in case of an error (e.g. parameter is null) */ public static int getDaysBetween(Date start, Date end) { if (start == null || end == null) { return -1; } Calendar startDate = convertDate(start); truncCalendar(startDate); Calendar endDate = convertDate(end); truncCalendar(endDate); long endL = endDate.getTimeInMillis() + endDate.getTimeZone().getOffset(endDate.getTimeInMillis()); long startL = startDate.getTimeInMillis() + startDate.getTimeZone().getOffset(startDate.getTimeInMillis()); int numDays = (int) ((endL - startL) / DAY_MILLIS); return Math.abs(numDays); } public static Calendar convertDate(Date d) { if (d == null) { return null; } Calendar c = Calendar.getInstance(); c.setTime(d); return c; } /** * truncate the calendar to a day with time 00:00:00.000 */ public static void truncCalendar(Calendar c) { if (c == null) { return; } c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); } }