Here you can find the source of getManyWeeksDifference(Date a, Date b)
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. |
public static int getManyWeeksDifference(Date a, Date b)
//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; } }