2つの時間の区間内に何週間あるかを計算します.

1410 ワード


public class TestUtil {

	public static int howManyDayOfWeek(String startDateStr, String endDateStr, int dayOfWeek) {
		int manyDayOfWeek = 0;
		Calendar calStart = getCalendar(startDateStr, "-");
		Calendar calEnd = getCalendar(endDateStr, "-");
		int balance = dayOfWeek - calStart.get(Calendar.DAY_OF_WEEK);
		// 
		calStart.add(Calendar.DATE, balance);
		// 
		if (balance < 0) {
			calStart.add(Calendar.WEEK_OF_MONTH, 1);
		}
		while (calStart.before(calEnd) || calStart.equals(calEnd)) {
			calStart.add(Calendar.WEEK_OF_MONTH, 1);
			manyDayOfWeek++;
		}
		return manyDayOfWeek;
	}
	
	public static void main(String[] args) {
		//1 ,2 , 
		int dayOfWeek = 1;
		System.out.println(TestUtil.howManyDayOfWeek("2009-03-01", "2009-03-31", dayOfWeek)+" ");
	}
	/**
	 * @deprecated  
	 * @param dateStr
	 * @param splitor
	 * @return
	 */
	private static Calendar getCalendar(String dateStr, String splitor) {
		Calendar cal = Calendar.getInstance();
		String[] dateArr = dateStr.split(splitor);
		cal.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]) - 1, Integer.parseInt(dateArr[2]));
		
		return cal;
	}

}