javascript算法在java中将月份中的天数列为下拉列表
我有两个下拉列表,一个表示月份,另一个表示天数
我的月份下拉列表如下:
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;
}
几天来,地图就像
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 to 30)),(5,(1 to 31)
等,直到3月]`
我使用核心java来编写这些部分代码。对于ui部分,使用jquery和javascript。SpringMVC是使用的技术。我一整天都在努力寻找一个可行的解决方案。任何帮助都将是非常可观的
# 1 楼答案
爪哇。时间
在Java8和更高版本中,使用java.time框架。{a2}{a3}知道每个月的天数
如果您需要当前年度的天数,请使用^{} 获取date实例,并使用其简单的^{} 和^{} 方法,直到您收集了该年度的所有月份。然后使用相同的API获取这些月份的天数
# 2 楼答案
参见日历#getActualMaximum:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getActualMaximum(int)
它会让你知道每个月有多少天
@Arun nagar您的代码已损坏,无法在2100年使用(可除以4,但不是闰年)。我不能投反对票,因为我没有足够的声誉,但我会的
# 3 楼答案
你还必须关心闰年和其他。。。因此,创建一个} 不需要重新创建weel
Calendar
,设置year
和month
并使用^{附加组件:
在此之后,只需在实际的
for-loop
中使用daysInMonth + 1
当然,最好创建一个函数来封装和替换2行
把它叫做:
getNumberOfDays(Calendar.FEBRUARY, 2016);