Java Day End secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)

Here you can find the source of secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)

Description

seconds Between

License

Open Source License

Declaration

public static int secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour) 

Method Source Code

//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;
    }
}

Related

  1. isTodayWithinPeriod(Date periodStart, Date periodEnd)
  2. isValidSendDate(Date sendDate)
  3. isWorkingDay(final Calendar calendar, final Long[] holidays)
  4. postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount, final Long[] holidays)
  5. previousWorkingDay(final Calendar calendar, final Long[] holidays)
  6. secondsToEndOfDay(Date aDate)
  7. secondsToEndOfDay(Date aDate)
  8. setEndDay(Date date)
  9. setTimeToEndofDay(Calendar calendar)