Here you can find the source of secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)
public static int secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)
//package com.java2s; /******************************************************************************* * Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com * 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 ww w .j a va2s .c o m*/ * emil.crumhorn@gmail.com - initial API and implementation *******************************************************************************/ import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main { private static Locale _locale; public static int secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour) { Calendar sDate = Calendar.getInstance(_locale); Calendar eDate = Calendar.getInstance(_locale); sDate.setTime(start); eDate.setTime(end); if (assumeSameDate) { // set same date eDate.set(Calendar.YEAR, 2000); sDate.set(Calendar.YEAR, 2000); eDate.set(Calendar.DAY_OF_YEAR, 1); sDate.set(Calendar.DAY_OF_YEAR, 1); } if (assumeSameHour) { eDate.set(Calendar.HOUR, 1); sDate.set(Calendar.HOUR, 1); } long diff = eDate.getTimeInMillis() - sDate.getTimeInMillis(); diff /= 1000; return (int) diff; } }