/******
* 获取当前时间三个月内的日期
* @return
*/
public static JSONObject getThreeDays(){
SimpleDateFormat format = new SimpleDateFormat("MM-dd");
SimpleDateFormat formatt = new SimpleDateFormat("MM月dd日");
Calendar startCalendar = Calendar.getInstance(),
endCalendar = Calendar.getInstance();
JSONObject jsonObject = new JSONObject() ;
JSONArray dateArr = new JSONArray();
try {
startCalendar.setTime(format.parse(format.format(new Date())));
endCalendar.setTime(format.parse(format.format(getLastMonth(new Date()))));
do{
UtilBean utilBean = new UtilBean();
utilBean.setKey(format.format(startCalendar.getTime()));
utilBean.setValue(formatt.format(startCalendar.getTime()));
dateArr.add(utilBean);
startCalendar.add(Calendar.DAY_OF_MONTH, 1);
}while(!startCalendar.after(endCalendar));
jsonObject.put("days", dateArr);
} catch (ParseException e) {
e.printStackTrace();
}
return jsonObject;
}
//获取今天开始的三个月后的日期
public static Date getLastMonth(Date date){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,3);
Date otherDate=cal.getTime();
return otherDate;
}
action的代码
/**
* 获取日期的action
*/
@Action("getDate")
public String getDate(){
JSONObject jb = DateUtils.getThreeDays();
super.setJsonText(jb.toString());
return JSONPG;
}
页面代码
function loadDays() {
$.ajax({
url: '/slark/front/date/getDate.do',
type: 'POST',
dataType: 'JSON',
success: function (data) {
for(var i in data.days){
$("#startTime").append("<option value="+data.days[i].key+">"+data.days[i].value+"</option>");
}
}
});
}
<select id="startTime" >
</select>