获取指定月 获取指定日期是星期几

	public static List getByMonth(String date){
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(dateFormat.parse(date));
		} catch (ParseException e) {
			// TODO: handle exception
			return null;
		}
		int firstDay=c.getMinimum(Calendar.DAY_OF_MONTH);
		int endDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		List list=new ArrayList();
		for (int i = firstDay; i <= endDay; i++) {
			c.set(Calendar.DAY_OF_MONTH, i);
			//c.add(Calendar.DAY_OF_MONTH, 1);
			String dayOfWeek=getWeekOfDate(c.getTime());
			int weekint=c.get(Calendar.WEEK_OF_MONTH);
			String weeks="";
			weeks=getweekOfMonth(weekint);
			String day=String.valueOf(i);
			int index=weekOfExist(weeks,list);
			if(index==-1){
				ExcelDateHead excelDateHead=new ExcelDateHead();
				excelDateHead.setWeekOfMonth(weeks);
				ExcelDateBody excelDateBody=new ExcelDateBody();
				excelDateBody.setDay(day);
				excelDateBody.setWeek(dayOfWeek);
				excelDateBody.setDetail(c.getTime());
				excelDateHead.getBody().add(excelDateBody);
				list.add(excelDateHead);
			}else {
				ExcelDateBody excelDateBody=new ExcelDateBody();
				excelDateBody.setWeek(dayOfWeek);
				excelDateBody.setDay(day);
				excelDateBody.setDetail(c.getTime());
				list.get(index).getBody().add(excelDateBody);
			}
		}
		return list;
	}




	/** * 获取指定日期是星期几
	  * 参数为null时表示获取当前日期是星期几
	  * @param date
	  * @return
	*/
	public static String getWeekOfDate(Date date) {      
	    String[] weekOfDays = {"日", "一", "二", "三", "四", "五", "六"};        
	    Calendar calendar = Calendar.getInstance();      
	    if(date != null){        
	         calendar.setTime(date);      
	    }        
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0){        
	        w = 0;      
	    }      
	    return weekOfDays[w];    
	}


	public static String getweekOfMonth(int week){
		String[] weeks={"第一周","第二周","第三周","第四周","第五周","第六周"};
		//System.out.println(week);
		int a=week-1;
		if(a<0){
			a=0;
		}
		return weeks[a];
	}

	public static int weekOfExist(String weeks,List list){
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getWeekOfMonth().equals(weeks)){
				return i;
			}
		}
		return -1;
	}

  

你可能感兴趣的:(获取指定月 获取指定日期是星期几)