我有两个下拉列表,一个表示月份,另一个表示日期。
我的月份下拉列表如下,
public Map<Integer, String> month(){
final Map<Integer, String> month = new LinkedHashMap<Integer, String>();
month.put(4,"April");
month.put(5,"May");
month.put(6,"June");
month.put(7,"July");
month.put(8,"August");
month.put(9, "September");
month.put(10,"October");
month.put(11,"November");
month.put(12,"December");
month.put(1, "January");
month.put(2,"February");
month.put(3, "March");
return monthmap;
}
几天来 map 就像这样
public Map<Integer, Integer> dayOfMonth(){
Map<Integer, Integer> day = new LinkedHashMap<Integer, Integer>();
//In this section feasible algorithm required.
for(int i=1;i<32;i++)
{
day.put(i,i);
}
return day;
}
基于这个算法,我得到了每个月的 31 天。但是我只需要相应月份的正确日期。例如:“六月只需要 30 天,在这种情况下它有 31 天”。所以我需要的是基于月图,所有相应的日期应插入到日图内。
例如[(4,(1到30)),(5,(1到31)
等等直到3月]。`
我在这些部分代码中使用核心java。对于ui部分,使用了jquery和javascript。使用的技术是Spring MVC。我一整天都在努力寻找可行的解决方案。任何帮助都将非常感激.. ...
最佳答案
您还必须关心闰年和其他...因此创建一个日历
,设置年份
和月份
并使用getActualMaximum
来不需要重新发明威尔。
int iYear = 2016;
int iMonth = Calendar.FEBRUARY;
int iDay = 1;
// Calendar object and set year and month
Calendar cal = new GregorianCalendar(iYear, iMonth, iDay);
// Number of days in that month
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29 !!!
附加组件:
此后,只需在实际的
for 循环
中使用daysInMonth + 1
即可。for(int i=1; i < daysInMonth + 1; i++)
当然,最好创建一个函数来封装并替换2行
private int getNumberOfDays(int month, int year) { Calendar cal = new GregorianCalendar(year, month, 1); return cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29 !!! }
并这样调用它:
getNumberOfDays(Calendar.FEBRUARY, 2016);
。
关于javascript - 在java中将月份中的天数列为下拉列表的可行算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392945/