我正在尝试将所有 12 个月都放入 java 中的字符串列表中。如何使用 java.util 日历执行此操作。我已经使用以下代码来构建年度列表!现在我想要的是为 12 个月建立一个月份列表。
protected String getYears(){
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 3);
int futureYear = cal.get(Calendar.YEAR);
List<String> years = new ArrayList<String>();
for(int i = -2 ; i < 1 ; i++){
if(i != 0){
cal = Calendar.getInstance();
cal.add(Calendar.YEAR,i);
years.add(Integer.toString(cal.get(Calendar.YEAR)));
}else if(currentYear != futureYear){
years.add(Integer.toString(currentYear));
years.add(Integer.toString(futureYear));
}else if(currentYear == futureYear){
years.add(Integer.toString(currentYear));
}
}
HTMLOptionBuilder ob = new HTMLOptionBuilder(false);
for(int i = 0; i < years.size(); i++){
if(years.get(i).equals(currentYear)){
ob.addOption(years.get(i), years.get(i), true, true);
}else{
ob.addOption(years.get(i), years.get(i));
}
}
return ob.getHTML();
}
请帮我!谢谢你!