Java Day Between getManyWeeksDifference(Date a, Date b)

Here you can find the source of getManyWeeksDifference(Date a, Date b)

Description

get Many Weeks Difference

License

Open Source License

Parameter

Parameter Description
a One date. Sequential order with other date parameter does not matter.
b Another date. Sequential order with other date parameter does not matter.

Return

Returns the number of weeks difference between Date a and Date b.

Declaration

public static int getManyWeeksDifference(Date a, Date b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Boeing./* w w w . j a v a  2s  . co  m*/
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * @param a One date. Sequential order with other date parameter does not matter.
     * @param b Another date. Sequential order with other date parameter does not matter.
     * @return Returns the number of weeks difference between Date a and Date b.
     */
    public static int getManyWeeksDifference(Date a, Date b) {
        int weeks = 0;
        Calendar aCal = Calendar.getInstance();
        Calendar bCal = Calendar.getInstance();
        aCal.setTime(a);
        bCal.setTime(b);

        Calendar startCal, endCal;
        if (aCal.before(bCal)) {
            startCal = aCal;
            endCal = bCal;
        } else {
            startCal = bCal;
            endCal = aCal;
        }

        while (startCal.before(endCal)) {
            startCal.add(Calendar.WEEK_OF_YEAR, 1);
            weeks++;
        }

        return weeks;
    }
}

Related

  1. getDifference(Date d1, Date d2)
  2. getDifferenceInDays(final Date startDate, final Date endDate)
  3. getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat)
  4. getDifferencesBetweenIndicateDays( final Date minorDate, final Date majorDate)
  5. getDiffMilliSeconds(Date form, Date to)
  6. getMinuteDiffByTime(Date time1, Date time2)
  7. getMinutesDifference(final Date begin, final Date end)
  8. getMonthDifference(Date from, Date to)
  9. getMonthsDifference(Date earlierDate, Date laterDate)