日期:2014-05-20 浏览次数:20766 次
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class Test_11 {
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
/**
* 测试用例
*/
public static void main(String[] args) {
List<String> days1 = findDays_1(new Date());
for(String day : days1){
System.out.println(day);
}
System.out.println("-------- 我是分割线 -----------");
List<String> days2 = findDays_2(2013);
for(String day : days2){
System.out.println(day);
}
}
public static List<String> findDays_1(Date day){
ArrayList<String> days = new ArrayList<String>();
Calendar cal = Calendar.getInstance();
cal.setTime(day);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_MONTH, 1);
Date end = cal.getTime();
cal.setTime(day);
cal.add(Calendar.YEAR, -1);
Date cur = null;
do{
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
// Date monday = cal.getTime();
days.add(SDF.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 6);
cur = cal.getTime();
days.add(SDF.format(cur));
}while(cur.before(end));
return days;
}
public static List<String> findDays_2(int year){
ArrayList<String> days = new ArrayList<String>();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
for(int i=0;i<12;i++){
days.add(SDF.format(cal.getTime()));
cal.add(Calendar.MONTH, 1);