计算两个日期间相隔了多少周

       

       计算两个日期间相隔多少周,只要个了周日就算一周              

	/**

	 * 计算两个日期间相隔的周数

	 * 

	 * @param startDate

	 *            开始日期

	 * @param endDate

	 *            结束日期

	 * @return

	 */

	public static int computeWeeks(Date startDate, Date endDate) {



		int weeks = 0;



		Calendar beginCalendar = Calendar.getInstance();

		beginCalendar.setTime(startDate);



		Calendar endCalendar = Calendar.getInstance();

		endCalendar.setTime(endDate);



		while (beginCalendar.before(endCalendar)) {



			// 如果开始日期和结束日期在同年、同月且当前月的同一周时结束循环

			if (beginCalendar.get(Calendar.YEAR) == endCalendar

					.get(Calendar.YEAR)

					&& beginCalendar.get(Calendar.MONTH) == endCalendar

							.get(Calendar.MONTH)

					&& beginCalendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) == endCalendar

							.get(Calendar.DAY_OF_WEEK_IN_MONTH)) {

				break;



			} else {



				beginCalendar.add(Calendar.DAY_OF_YEAR, 7);

				weeks += 1;

			}

		}



		return weeks;

	}

你可能感兴趣的:(Java)