/*
* 當前時間向前推num個月,返回不重複的年份月份list
* 首先生成set集合,第一個set是年份,第二個set是月份,最後再把set轉換成List便可
*
* */
public static List<List<Integer>> getList(Integer num){
List<List<Integer>> list=new ArrayList<List<Integer>>();
List<Integer> list1=new ArrayList<Integer>();
List<Integer> list2=new ArrayList<Integer>();
TreeSet<Integer> setYear = new TreeSet<Integer>();
TreeSet<Integer> setMonth = new TreeSet<Integer>();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
cal.add(Calendar.MONTH, -num);
int yearStart = cal.get(Calendar.YEAR);
int monthStart = cal.get(Calendar.MONTH) + 1;
System.out.println("起始年份:"+yearStart+" 起始月份:"+monthStart+" 當前年份:"+yearNow+" 當前月份"+monthNow);
for(int i=1;i<=num;i++){
cal.add(Calendar.MONTH, -1);
Integer y = cal.get(Calendar.YEAR);
Integer m = cal.get(Calendar.MONTH) + 1;
setYear.add(y);
setMonth.add(m);
}
//setYear放入list1,setMonth放入list2
list1.addAll(setYear);
list2.addAll(setMonth);
//將兩個list放入總list
list.add(list1);
list.add(list2);
System.out.println("年份集合:"+setYear+" 月份集合:"+setMonth);
System.out.println(list);
return list;
}get