当前位置:编程学习 > JAVA >>

java怎么统计出当年有多少个周,并列出那些周的日期

最近做个东西,要求统计出当年有多少个周,并列出那些周的日期,百度一把发现
只有计算当前日期的是第几周,int currentWeekOfYear = cal.get(Calendar.WEEK_OF_YEAR);

那位大神,给小弟说道,怎么弄呢?谢谢 --------------------编程问答--------------------
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WeekList {

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static String[] getWeeksInYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year+1);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 1);
cal.add(Calendar.DAY_OF_MONTH, -(6+cal.get(Calendar.DAY_OF_WEEK)));
int weeks = cal.get(Calendar.WEEK_OF_YEAR);
String[] days = new String[weeks];
for(int i=weeks-1;i>=0;i--){
days[i] = sdf.format(cal.getTime());
cal.add(Calendar.WEEK_OF_YEAR, -1);
}
return days;
}
/**
 * 测试用例
 */
public static void main(String[] args) {
int year = 2013;
String[] days = getWeeksInYear(year);
for(String day:days){
System.out.println(day);
}
}

}
--------------------编程问答-------------------- 结果
2012-12-30
2013-01-06
2013-01-13
2013-01-20
2013-01-27
2013-02-03
2013-02-10
2013-02-17
2013-02-24
2013-03-03
2013-03-10
2013-03-17
2013-03-24
2013-03-31
2013-04-07
2013-04-14
2013-04-21
2013-04-28
2013-05-05
2013-05-12
2013-05-19
2013-05-26
2013-06-02
2013-06-09
2013-06-16
2013-06-23
2013-06-30
2013-07-07
2013-07-14
2013-07-21
2013-07-28
2013-08-04
2013-08-11
2013-08-18
2013-08-25
2013-09-01
2013-09-08
2013-09-15
2013-09-22
2013-09-29
2013-10-06
2013-10-13
2013-10-20
2013-10-27
2013-11-03
2013-11-10
2013-11-17
2013-11-24
2013-12-01
2013-12-08
2013-12-15
2013-12-22
--------------------编程问答-------------------- 列出那些周的日期 ?什么意思? --------------------编程问答--------------------
引用 1 楼 preferme 的回复:
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WeekList {

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static String[] getWeeksInYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year+1);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 1);
cal.add(Calendar.DAY_OF_MONTH, -(6+cal.get(Calendar.DAY_OF_WEEK)));
int weeks = cal.get(Calendar.WEEK_OF_YEAR);
String[] days = new String[weeks];
for(int i=weeks-1;i>=0;i--){
days[i] = sdf.format(cal.getTime());
cal.add(Calendar.WEEK_OF_YEAR, -1);
}
return days;
}
/**
 * 测试用例
 */
public static void main(String[] args) {
int year = 2013;
String[] days = getWeeksInYear(year);
for(String day:days){
System.out.println(day);
}
}

}

--------------------编程问答-------------------- 每年都有52周吧。。。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,