Calendar thisMonth = Calendar.getInstance();
thisMonth.set(Calendar.YEAR, 2002);
thisMonth.set(Calendar.MONTH, 07);//指2002-8月
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
thisMonth.set(Calendar.DAY_OF_MONTH,1);
int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK);
现在取得的firstIndex就是4,自己再改改就OK啦。
thisMonth.set(Calendar.YEAR, 2002);
thisMonth.set(Calendar.MONTH, 07);//指2002-8月
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
thisMonth.set(Calendar.DAY_OF_MONTH,1);
int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK);
现在取得的firstIndex就是4,自己再改改就OK啦。
thisMonth.set(Calendar.MONTH, 07);//指2002-8月
6月应该是
thisMonth.set(Calendar.MONTH, 05)
MONTH是从0开始计数的。
你上面的代码只是得到了某一天是星期几,但是知道第几周怎么得到是哪一天?如2002年8月第一周的星期一是哪天?
thisMonth.set(Calendar.YEAR, 2002);
thisMonth.set(Calendar.MONTH, 07);//指2002-8月
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
thisMonth.set(Calendar.DAY_OF_MONTH,1);
int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK)-1;
int maxIndex=thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
int kk = 0;
int yourWantWeek = 2;
int yourWantWeekDay = 3; //如上表示你想要的是2002-08的第二个的星期三。
int i=1;
System.out.println(firstIndex);
for(;i<maxIndex;i++){
int xx = firstIndex - (firstIndex/7)*7;
if(xx==yourWantWeekDay)
kk++;
if(kk==yourWantWeek){
break;
}
firstIndex++;
}
这里的i就是你所想要的。
thisMonth.set(Calendar.YEAR, 2002);
thisMonth.set(Calendar.MONTH, 07);//指2002-8月
thisMonth.set(Calendar.DAY_OF_MONTH,1);
int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK);上面不变,推测出8.1是星期几!如果刚好等于0则就是8.1,如果不是再根据
从星期1到星期7分别是0-6,加上相应的数字,就可以得到星期一的天数
或者在循环里如果是0的话再返回他的那天!