Java Day Between getDaysBetween(Date start, Date end)

Here you can find the source of getDaysBetween(Date start, Date end)

Description

Calculates the number of days between start and end.

License

Open Source License

Declaration

public static int getDaysBetween(Date start, Date end) 

Method Source Code

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

Related

  1. getDateDifferenceinDays(Date date1, Date date2)
  2. getDateDiffYear(Date date, int diff, String... format)
  3. getDateWithDiff(Date date, int amount)
  4. getDaysBetween(Date date1, Date date2)
  5. getDaysBetween(Date start, Date end)
  6. getDaysBetween(Date startDate, Date endDate)
  7. getDaysBetween(Date startDate, Date endDate)
  8. getDaysBetweenDate(Date begin, Date end)
  9. getDaysDiff(Date startDate, Date endDate)