Here you can find the source of daysBetween(Date startDate, Date endDate)
public static long daysBetween(Date startDate, Date endDate)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se). * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0-standalone.html ******************************************************************************/ import java.util.Calendar; import java.util.Date; public class Main { public static long daysBetween(Date startDate, Date endDate) { Calendar start = Calendar.getInstance(); start.setTime(startDate);/*from w w w . j ava 2 s . c om*/ Calendar end = Calendar.getInstance(); end.setTime(endDate); return daysBetween(start, end); } public static long daysBetween(Calendar startDate, Calendar endDate) { startDate = (Calendar) startDate.clone(); long daysBetween = 0; while (startDate.get(Calendar.YEAR) < endDate.get(Calendar.YEAR)) { if (startDate.get(Calendar.DAY_OF_YEAR) != 1) { int diff = startDate.getMaximum(Calendar.DAY_OF_YEAR) - startDate.get(Calendar.DAY_OF_YEAR); diff++; startDate.add(Calendar.DAY_OF_YEAR, diff); daysBetween += diff; } else { daysBetween += startDate.getMaximum(Calendar.DAY_OF_YEAR); startDate.add(Calendar.YEAR, 1); } } daysBetween += endDate.get(Calendar.DAY_OF_YEAR) - startDate.get(Calendar.DAY_OF_YEAR); return daysBetween; } }