Java时间参数作为查询参数

 String endTime = eendDate;
           String startDate2 = estartDate;
            utilDate = sdf.parse(endTime);
            startUtilDate = sdf.parse(startDate2);
            endDate = new java.sql.Date(utilDate.getTime());
            startDate = new java.sql.Date(startUtilDate.getTime());

以上Java将 String类型转为date类型,数据库查询的话

 @ApiOperation(value = "聊天回复内容统计", notes = "回复文本与非文本消息")
    @RequestMapping(value = "/nothing/robot/count/textOrNot", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "String",value = "字符串时间格式为:<br>yyyy-MM-dd 比如:2018-07-16",name = "estartDate"),
            @ApiImplicitParam(paramType = "query",dataType = "String",value = "字符串时间格式为:<br>yyyy-MM-dd 比如:2018-07-16",name = "eendDate")
    })
    public ResponseResult getChatislanCount(@RequestParam (required = false)String estartDate,@RequestParam (required = false) String eendDate) throws ParseException {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState(ResultCode.SUCCESS.getValue());

        // 将字符串转为java.sql.date类型
        //1,首先字符串转为date类型
        //2,再将date转为java.sql.date类型
//        String estartDate=queryDate.getEstartDate();
//        String eendDate=queryDate.getEendDate();
        Date utilDate = null;
        Date startUtilDate = null;
        java.sql.Date startDate = null;
        java.sql.Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        if (estartDate != null && eendDate != null) {
            String endTime = eendDate;
            String startDate2 = estartDate;
            utilDate = sdf.parse(endTime);
            startUtilDate = sdf.parse(startDate2);
            endDate = new java.sql.Date(utilDate.getTime());
            startDate = new java.sql.Date(startUtilDate.getTime());
        } else {
            String endTime = sdf.format(new Date());
            Date beginDate = new Date();
            Calendar date2 = Calendar.getInstance();
            date2.setTime(beginDate);
            date2.set(Calendar.DATE, date2.get(Calendar.DATE) - 3);
            String startDate2 = sdf.format(date2.getTime());
            utilDate = sdf.parse(endTime);
            startUtilDate = sdf.parse(startDate2);
            endDate = new java.sql.Date(utilDate.getTime());
            startDate = new java.sql.Date(startUtilDate.getTime());
        }

        responseResult.setData(robotCountService.findChatTextOrNotText(startDate, endDate));
        return responseResult;
    }
@Override
    public Map<String,Object> findChatTextOrNotText(java.sql.Date startDate,java.sql.Date endDate) {
        String sql;
        sql="select date_format(create_time,'%m-%d') as days, \n" +
                "sum(case when (content_type=4 and reply_content_type= -5 )then 1 else 0 end) 'islan',\n" +
                "sum(case when reply_content_type=-7 then 1 else 0 end) 'welcome',\n" +
                "sum(case  when reply_content_type =-9 then 1 else 0 end) 'ownlink',\n" +
                "sum(case when reply_content_type=-1 then 1 else 0 end) 'jitang',\n" +
                "sum(case when reply_content_type=-2 then 1 else 0 end) 'naoji',\n" +
                "sum(case when reply_content_type=-3 then 1 else 0 end) 'dianzan',\n" +
                "sum( case when reply_content_type=-4 then 1 else 0 end ) 'biaoqing',\n" +
                "sum( case when reply_content_type=-6 then 1 else 0 end)'tietu',\n" +
                "sum( case when reply_content_type=-8 then 1 else 0 end) 'dongtu',\n" +
                "sum( case when reply_content_type=-10 then 1 else 0 end) 'meinvtu',\n" +
                "sum( case when reply_content_type=-11 then 1 else 0 end) 'lineone',\n" +
                "sum( case when reply_content_type=-12 then 1 else 0 end) 'linesix',\n" +
                "sum( case when reply_content_type=-13 then 1 else 0 end) 'wehat'\n" +
                "from robotapi.messenger_record \n" +
                "where  date(create_time) >='"+startDate+"' and date(create_time)<='"+endDate+"' group by date_format(create_time,'%Y%m%d');";



//        System.out.println(sql);
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        int total=result.size();
        return toResult(result,total);
    }

方法二:

 @ApiOperation(value = "最近聊天", notes = "时间格式:2018-07-16")
    @RequestMapping(value = "/nothing/robot/count/getLaterCunts", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "date", value = "输入的时间格式为:<br>2018-07-16"),
    })
    public ResponseResult getLaterCunts(@RequestParam String date) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState(ResultCode.SUCCESS.getValue());
        responseResult.setData(robotCountService.findLaterCount(date));
        return responseResult;
    }
  @Override
    public Map<String, Object> findLaterCount(String queryTime) {

        // str_to_date('"+queryTime+"','%Y-%m-%d') 中的时间格式是可以变的也可以是%Y/%m/%d
        String sql = "select date_format(create_time,'%m-%d') as days, \n" +
                "sum(case when (content_type=4 and reply_content_type= -5 )then 1 else 0 end) 'islan',\n" +
                "sum(case when reply_content_type=-7 then 1 else 0 end) 'welcome',\n" +
                "sum( case when reply_content_type=-13 then 1 else 0 end) 'wehat'\n" +
                "from robotapi.messenger_record \n" +
                "where  date(create_time)>=   str_to_date('"+queryTime+"','%Y-%m-%d')  group by date_format(create_time,'%Y%m%d');";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        int total=result.size();
        return toResult(result,total);
    }

直接用str_to_date('"+queryTime+"','%Y-%m-%d')  将String类型参数传进去即可查询。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值