参见英文答案 >
How can I build a list of days, months, years from a calendar object in Java? 5个
我试图将所有12个月的时间都放入java中的String列表中.如何使用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 years = new ArrayList();
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();
}
请帮我!
谢谢!