按每天24小时和按月每天的统计查询的实现

在进行一天按小时统计时,我们常常会面临数据库中只能查出有数据部分,如何让它默认为0,并且和x轴上的时间一一对应啦。

思路:首先我们定义一个数据长度为24,将数据库中查出的00时存入到数组下表23的位置。

其余要判断以0开头的,然后截取后面意味数字与数组中数字匹配,如果是两位数直接判断就好,废话不多数以代码凭

int Lose[] =newint[24];

                     while (rs.next()) {

                                   int index=0;

                                   if (rs.getString("starttime").startsWith("0")) {

                                          if(rs.getString("starttime").equals("00")){

                                                 Lose[23]=rs.getInt("outcalls");     

                                          }

                                          else{

                                          index Integer.parseInt(rs.getString("starttime").substring(1));

                                          Lose[index-1]=rs.getInt("outcalls");

                                          }

                                   }else{

                                          index= Integer.parseInt(rs.getString("starttime"));

                                          Lose[index-1]=rs.getInt("outcalls");

                                   }

                                  

                     }

然后将相应的数组对象放入map中,在转换为json格式输出,便可以实现输出。

同样在根据月份,查询30天的方法也是类似,但更为复杂一点点,我们先要获取最早查询日期,与数据库库中取出相差的天数,便为相应的数组下标

//计算两个日期之间的相差天数

    public static int differentDays(Stringdate1,String date2)

          {   int days=0;

               SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd");

               try

              {   Date date22 =format.parse(date2);

                   Date date = format.parse(date1);

                     days = (int)((date22.getTime()- date.getTime())/(1000*3600*24));

                 // System.out.println("两个日期的差距:" +days);

              } catch (ParseException e) {

                   e.printStackTrace();

              }

              return days;

          }

上面为计算两日期相差天数,也就是我们所需要的数组下标。

       int Lose[] =new int[30];

                     while (rs.next()) {

                            Stringstarttime=rs.getString("starttime");

                            int index=differentDays(forward30(starttimes),starttime);

                            Lose[index]=rs.getInt("nosums2");

                     }

forward30(starttimes)为获取最开始查询日期的函数。按照此方法便能较好的实现大屏显示数据的合规性。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值