以下代码关于上下周出现第0周,第5周的问题,请路过的大仙指导下???

streamlong 发布于 2011/08/11 18:31
阅读 351
收藏 0
//变量
private Integer currentYear;// 当前年
private Integer currentMonth;// 当年月
private Integer currentWeekOfMonth;// 当前月的当前周
private Integer currentWeekOfYear;// 当前月的当前周

/**
* 周偏移量,下周=1,上周=-1
 */
private int offsetWeek = 0;
private String ymonth = null;
private List<String[]> dayList = new ArrayList<String[]>();

//初始化日历
private void initCalendar(Calendar start) {
	start.setFirstDayOfWeek(Calendar.MONDAY);
	start.setMinimalDaysInFirstWeek(5); 
}
//判断下一周
private boolean nextWeek(Calendar start) {
	Calendar theDay = (Calendar) start.clone();
	initCalendar(theDay);
	int weekOfMonth = theDay.get(Calendar.WEEK_OF_MONTH);
	theDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
	int weekOfMonth2 =theDay.get(Calendar.WEEK_OF_MONTH);
	if (weekOfMonth != 1 && weekOfMonth2 == 1) {
		return true;
	}
	return false;
}
//setDay()
private void setDay(Calendar start) {
	if (currentWeekOfYear != null)
		return;

	initCalendar(start);

	start.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
	if (ymonth != null && !"".equals(ymonth)) {
		Date yue = null;
		try {
			yue = DATE_FORMAT.parse(ymonth);
		} catch (Exception e) {
			yue = new Date();
		}
		start.setTime(yue);
		start.set(Calendar.HOUR_OF_DAY, 12); 
		start.set(Calendar.WEEK_OF_MONTH, 1); 
	}
		
	start.add(Calendar.WEEK_OF_YEAR, offsetWeek);

	settingList(start);

	if (nextWeek(start))
	start.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
	
	currentYear = start.get(Calendar.YEAR);
	currentMonth = start.get(Calendar.MONTH);
	currentWeekOfMonth = start.get(Calendar.WEEK_OF_MONTH);
		currentWeekOfYear = start.get(Calendar.WEEK_OF_YEAR);
	}

//settingList()

private void settingList(Calendar _start) {
	Calendar start = (Calendar) _start.clone();
	initCalendar(start);
	start.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
	Calendar end = (Calendar) _start.clone();
	initCalendar(end);
	end.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

	log.info("start=" + start.getTime() + ",end=" + end.getTime());
	Long[] hDays = WorkDayCalculator.newInstance()
				.getHolidays(start.getTime(), end.getTime())
				.toArray(new Long[0]);

	dayList = new ArrayList<String[]>();
	SimpleDateFormat sdf = new SimpleDateFormat("M月d日");
	for (int i = 0; i < 7; i++) {
	  long time = WorkDayCalculator.pureTime(start);
		if (Arrays.binarySearch(hDays, time) >= 0) {
			start.add(Calendar.DAY_OF_YEAR, 1);
				continue;
			}
			String weeks = null, field = null;
			switch (i) {
			case 0:
				weeks = "星期一";
				field = "mon";
				break;
			case 1:
				weeks = "星期二";
				field = "tue";
				break;
			case 2:
				weeks = "星期三";
				field = "wed";
				break;
			case 3:
				weeks = "星期四";
				field = "thu";
				break;
			case 4:
				weeks = "星期五";
				field = "fri";
				break;
			case 5:
				weeks = "星期六";
				field = "sta";
				break;
			case 6:
				weeks = "星期日";
				field = "sun";
				break;
			}
	        dayList.add(new String[] { sdf.format(start.getTime()), weeks,
					field });
		start.add(Calendar.DAY_OF_YEAR, 1);
		}
	
这段代码出了我上一个问题描述的第0周,第5周的情况.setDay()是方法的入口
请问上面的代码严密吗???我使用Calendar类使用不多,请路过的各位大仙指导下???
加载中
返回顶部
顶部