获取今天开始之后三个月的日期列表

本文提供了一个Java方法,用于获取当前时间三个月内的日期,并通过AJAX请求将这些日期展示在一个下拉选择框中。
 /******
      * 获取当前时间三个月内的日期
      * @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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值