Java Day Between getDateDifference(Date date1, Date date2)

Here you can find the source of getDateDifference(Date date1, Date date2)

Description

Returns number of days between date1 and date2, such that date1 + days = date2

License

Open Source License

Parameter

Parameter Description
date1 a parameter
date2 a parameter

Declaration

public static int getDateDifference(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * QMetry Automation Framework provides a powerful and versatile platform to author 
 * Automated Test Cases in Behavior Driven, Keyword Driven or Code Driven approach
 *                /*  w w  w  .j  a  v a2  s .c o m*/
 * Copyright 2016 Infostretch Corporation
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
 *
 * You should have received a copy of the GNU General Public License along with this program in the name of LICENSE.txt in the root folder of the distribution. If not, see https://opensource.org/licenses/gpl-3.0.html
 *
 * See the NOTICE.TXT file in root folder of this source files distribution 
 * for additional information regarding copyright ownership and licenses
 * of other open source software / files used by QMetry Automation Framework.
 *
 * For any inquiry or need additional information, please contact support-qaf@infostretch.com
 *******************************************************************************/

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

public class Main {
    /**
     * Returns number of days between date1 and date2, such that date1 + days =
     * date2
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int getDateDifference(Date date1, Date date2) {
        int oneDay = 1000 * 60 * 60 * 24;
        Calendar cal1 = getCalendarDate(date1);
        Calendar cal2 = getCalendarDate(date2);

        return (int) ((cal1.getTimeInMillis() - cal2.getTimeInMillis()) / oneDay);
    }

    public static Calendar getCalendarDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
}

Related

  1. getBetweenDays(long longValue1, long longValue2)
  2. getBetweenDays(String start, String end)
  3. getBetweenTwoDayCalender(String startDay, String endDay)
  4. getDateDiff(Date date, int day)
  5. getDateDiff(final Date startDate, final Date endDate)
  6. getDateDifferenceinDays(Date date1, Date date2)
  7. getDateDiffYear(Date date, int diff, String... format)
  8. getDateWithDiff(Date date, int amount)
  9. getDaysBetween(Date date1, Date date2)