给出任意年月,得出这个月每周的起止时间例如:2011年01月算出第一周: 2011-1-1 至 2111-1-2第二周: 2011-1-3 至 2011-1-9第三周: 2011-1-10 至 2011-1-16第四周: 2011-1-17 至 2011-1-23第五周: 2011-1-24 至 2011-1-30第六周: 2011-1-31 至 2011-1-31如果没有第五周或第六周 ,则显示 8888-8-8 至 8888-8-8
给出任意年月,得出这个月每周的起止时间例如:2011年01月算出第一周: 2011-1-1 至 2111-1-2第二周: 2011-1-3 至 2011-1-9第三周: 2011-1-10 至 2011-1-16第四周: 2011-1-17 至 2011-1-23第五周: 2011-1-24 至 2011-1-30第六周: 2011-1-31 至 2011-1-31如果没有第五周或第六周 ,则显示 8888-8-8 至 8888-8-8
for example
import java.util.*;
import java.text.*;
class Test {
public static void main(String[] args) {
try {
String s = "2011-01"; //测试日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(s));
//c.set(Calendar.YEAR, 2011);
//c.set(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
int week = c.get(Calendar.WEEK_OF_MONTH);
int month = c.get(Calendar.MONTH);
String from = "", to = from;
while (true) {
if (c.get(Calendar.MONTH) != month) { //月份不同退出循环
System.out.printf("%d week: %s - %s\n", week, from, to);
break;
}
if (from == to) {
from = String.format("%tF", c.getTime());
}
to = String.format("%tF", c.getTime());
if (week != c.get(Calendar.WEEK_OF_MONTH)) {
System.out.printf("%d week: %s - %s\n", week, from, to);
from = to;
week++;
}
c.add(Calendar.DAY_OF_MONTH, 1);
}
for (week=week+1; week<7; week++) { //补足不满6周的
System.out.printf("%d week: 8888-8-8 - 8888-8-8\n", week);
}
} catch (Throwable e) {e.printStackTrace();}
}
}