Java Date Between daysBetween(final Date dateA, final Date dateB)

Here you can find the source of daysBetween(final Date dateA, final Date dateB)

Description

Calculates the number of days that have in common the first interval and the second interval

License

Open Source License

Declaration

public static int daysBetween(final Date dateA, final Date dateB) 

Method Source Code

//package com.java2s;
/**/*from   ww w . ja  va2s .  c  o m*/
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static final int YEAR = Calendar.YEAR;
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * Calculates the number of days that have in common the first interval and
     * the second interval
     */
    public static int daysBetween(final Date dateA, final Date dateB) {

        Date date1, date2;
        date1 = dateA;
        date2 = dateB;

        // Swap dates so that date1 <= date2
        if (date1.after(date2)) {
            date1 = dateB;
            date2 = dateA;
        }
        Calendar calendar1 = GregorianCalendar.getInstance();
        calendar1.setTime(date1);
        final Calendar calendar2 = GregorianCalendar.getInstance();
        calendar2.setTime(date2);

        int days = calendar2.get(java.util.Calendar.DAY_OF_YEAR) - calendar1.get(java.util.Calendar.DAY_OF_YEAR)
                + 1;
        final int y1 = calendar1.get(java.util.Calendar.YEAR);
        final int y2 = calendar2.get(java.util.Calendar.YEAR);
        if (y1 != y2) {
            calendar1 = (Calendar) calendar1.clone();
            do {
                days += calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
                calendar1.add(java.util.Calendar.YEAR, 1);
            } while (calendar1.get(java.util.Calendar.YEAR) != y2);
        }
        return days - 1;
    }

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }
}

Related

  1. daysBetween(Date smdate, Date bdate)
  2. daysBetween(Date smdate, Date bdate)
  3. daysBetween(Date smdate, Date bdate)
  4. daysBetween(Date startDate, Date endDate)
  5. daysBetween(final Date date1, final Date date2)
  6. daysBetween(String bdate, String edate)
  7. daysBetween(String beginDate, String endDate)
  8. daysBetween(String from, String to)
  9. daysBetween(String from, String to, String form)