信息来源:http://zhidao.baidu.com/question/146050582.html?qbl=relate_question_0&word=%C8%A1%B5%C3%B8%F8%B6%A8%CA%B1%BC%E4%B6%CE%C4%DA%B5%C4%CE%C4%BC%FE
问题:java如何得到指定时间段中的所有周,及每周的每一天都是周几何日期是多少?
参考答案如下:
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Test {
/**
* @param args
*/
public static void main(String[] args){
Calendar c_begin = new GregorianCalendar();
Calendar c_end = new GregorianCalendar();
DateFormatSymbols dfs = new DateFormatSymbols();
String[] weeks = dfs.getWeekdays();
c_begin.set(2010, 3, 2); //Calendar的月从0-11,所以4月是3.
c_end.set(2010, 4, 20); //Calendar的月从0-11,所以5月是4.
int count = 1;
c_end.add(Calendar.DAY_OF_YEAR, 1); //结束日期下滚一天是为了包含最后一天
while(c_begin.before(c_end)){
System.out.println("第"+count+"周 日期:"+new java.sql.Date(c_begin.getTime().getTime())+", "+weeks[c_begin.get(Calendar.DAY_OF_WEEK)]);
if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
count++;
}
c_begin.add(Calendar.DAY_OF_YEAR, 1);
}
}
}