例:今天 是2007-3-16号。
可以得到今天所在的周是本年的第11周,同时要把本月16号前,和16号的几周算出来。(注:一个月可能有4周,5周,6周三种)
[CODE]/*
* Test4.java
*
* Created on 2007年3月16日, 上午9:07
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test1;
import java.util.Calendar;
/**
*
* @author hadeslee
*/
public class Test4 {
/** Creates a new instance of Test4 */
public Test4() {
}
public static void main(String[] args) {
Calendar cal=Calendar.getInstance();
System.out.println("今天是本年第"+cal.get(Calendar.WEEK_OF_YEAR)+"周");
}
}[/CODE]
已解决,用了个笨点方法,不知道有无更好的:代码如下,你打印下就知道我说的什么意思了:
[CODE]
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class A {
public static void main(String[] args) throws IOException {
List weekOfYearList = new ArrayList();
Calendar c =new GregorianCalendar();
c.setTime(new Date());
c.set(Calendar.DATE,1);
c.setFirstDayOfWeek(Calendar.MONDAY);
int returnWeekFirst=c.get(Calendar.WEEK_OF_YEAR); //get first week of year;
weekOfYearList.add(new Integer(returnWeekFirst));
int dayOfWeek=c.get(Calendar.DAY_OF_WEEK)-1; //get first day of week
int maxDayOfMonth=c.getActualMaximum(Calendar.DAY_OF_MONTH);
int nextDayOfWeek=7-dayOfWeek;
//get other week of year
for(int i=nextDayOfWeek;i<maxDayOfMonth;i=i+7){
nextDayOfWeek=nextDayOfWeek+7;
c.set(Calendar.DATE,nextDayOfWeek);
c.setFirstDayOfWeek(Calendar.MONDAY);
int returnWeekNextDayOfWeek=c.get(Calendar.WEEK_OF_YEAR);
weekOfYearList.add(new Integer(returnWeekNextDayOfWeek));
}
/** ************print***************** **/
for(int j=0;j<weekOfYearList.size();j++){
System.out.println(weekOfYearList.get(j));
}
}
}
[/CODE]