Java笔记 ·

数据统计第一弹-按时/天/周/月补全某一段时间的数据-Java核心逻辑

本代码均结合之前的发布的DateUtil使用,之后的mysql查询部分看心情发布,就这么任性~ ~

StatisticResult 为要统计的数据实体类,根据自己情况自行替换。至于时间。。。就更不用说了吧O(∩_∩)O~

getNewStatisticResult()方法为创建新的StatisticResult,用于对应时间无数据时自动填充的数据。

一、按小时补全-具体逻辑

按小时补全-具体逻辑

/**
     * 按小时补全-具体逻辑
     * @param oldList
     * @return
     */
    public   List<StatisticResult> addHourForNull(List<StatisticResult> oldList){
        int hours = 24;
        int tmpLeng = oldList.size();
        int count = 0;
        int tmpHour = 0;
        ArrayList <StatisticResult> newList = new ArrayList<StatisticResult>();
        StatisticResult statisticResult = null;
        for(int i=0;i<24;i++){
            if(count<tmpLeng){
                tmpHour = Integer.parseInt(oldList.get(count).getTime());
                if(i==tmpHour){
                    newList.add(oldList.get(count));
                    count++;
                }else{
                    statisticResult=getNewStatisticResult(String.valueOf(i),oldList.get(count).getAction());
                    newList.add(statisticResult);
                }
            }else{
                statisticResult=getNewStatisticResult(String.valueOf(i),oldList.get(0).getAction());
                newList.add(statisticResult);
            }
        }
        return newList;
    }

二、按日补全数据-具体逻辑

按日补全-具体逻辑

 /**
     * 按日补全数据-具体逻辑
     * @param oldList
     * @param start
     * @param end
     * @return
     */
    public   List<StatisticResult> addDayForNull(List<StatisticResult> oldList,String start,String end){
        long n= DateUtil.getDaySub(start,end)+1;
        ArrayList <StatisticResult> newList = new ArrayList<StatisticResult>();
        int num=oldList.size();
        int temp=0;
        Date startDate = DateUtil.StringToDate(start,"yyyy-MM-dd");
        Date endDate = DateUtil.StringToDate(end,"yyyy-MM-dd");
        Date tmpDate = null;
        if(startDate==null||endDate==null){
            return newList;
        }
        StatisticResult dog = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<n;i++){
            if(temp < num){
                tmpDate = DateUtil.StringToDate(oldList.get(temp).getTime(),"yyyy-MM-dd");
                //如果开始日期小于当前记录日期则增加空白数据
                if(startDate.compareTo(tmpDate) < 0){
                    dog=getNewStatisticResult(dateFormat.format(startDate),oldList.get(temp).getAction());
                    newList.add(dog);
                }
                if(startDate.compareTo(tmpDate) == 0){//原数据加入
                    newList.add(oldList.get(temp));
                    temp++;
                }
            }else if(temp >= num && startDate.compareTo(endDate) <= 0){//加入空数据直到结束时间
                 dog=getNewStatisticResult(dateFormat.format(startDate),oldList.get(0).getAction());
                newList.add(dog);
            }
            //开始时间向前加一天
            startDate=DateUtil.addDateOneDay(startDate);
        }
        return newList;
    }

三、按周补全数据-整体逻辑

这个有点特殊,分了两个函数。

第一个根据起始用于计算每周的开始(不一定是周一,要看整体的开始时间是周几)与结束日期(一定是周日)。(注:整体的结束日期不一定是周日哦)

第二个函数用于每周数据的具体补全操作。

函数一:按周补全数据-整体逻辑

 /**
     * 按周补全数据-整体逻辑
     * @param oldList
     * @param start
     * @param end
     * @return
     */
    public   List<StatisticResult> addWeeksForNull(List<StatisticResult> oldList,String start,String end){
        Calendar   c_begin = DateUtil.StringtoCalendar(start,"yyyy-MM-dd");
        Calendar    c_end = DateUtil.StringtoCalendar(end,"yyyy-MM-dd");
        ArrayList <StatisticResult> newList = new ArrayList<StatisticResult>();
        if(c_begin==null||c_end==null){
            return newList;
        }
        Date c_tmp_begin = c_begin.getTime();
        Date c_tmp_end = c_begin.getTime();
        SimpleDateFormat  sdf  = new SimpleDateFormat("yyyy/MM/dd");
        int tmpLeng = oldList.size();
        int count = 0;
        StatisticResult statisticResult = null;
        StringBuffer tmpSTime = new StringBuffer();
        while(c_begin.compareTo(c_end)<=0){
//            System.out.println("第"+count+"周  日期:"+ sdf.format((c_begin.getTime().getTime())+","+weeks[c_begin.get(Calendar.DAY_OF_WEEK)]));
            if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
                //周日,标准的一周结束日期,进行一次数据判断添加
                c_tmp_end = c_begin.getTime();
                System.out.println("第"+count+"周 日期:"+sdf.format(c_tmp_begin.getTime())+"-"+sdf.format(c_tmp_end.getTime()));//用于查看一周开始与结束,可删
                if(tmpSTime.length()<=0){
                    tmpSTime.append(sdf.format(c_tmp_begin.getTime())+"-"+sdf.format(c_tmp_end.getTime()));
                }else{
                    tmpSTime.replace(0,tmpSTime.length(),sdf.format(c_tmp_begin.getTime())+"-"+sdf.format(c_tmp_end.getTime()));
                }
                count = addWeeksForNullOfNewList( count, tmpLeng,tmpSTime,oldList, newList, c_tmp_begin, c_tmp_end, statisticResult);
            }
            if(c_begin.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY){
                 //周一,标准的一周开始时间,使用c_tmp_begin临时记录,用于之后与结束日期组成“开始-结束”的时间字符串。
                c_tmp_begin = c_begin.getTime();
            }
            if(c_begin.compareTo(c_end)==0&&(c_begin.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY)){
                //最后一天,但结束日期不为周日时进行对应的判断与数据填充
                c_tmp_end = c_begin.getTime();
                if(tmpSTime.length()<=0){
                    tmpSTime.append(sdf.format(c_tmp_begin.getTime())+"-"+sdf.format(c_tmp_end.getTime()));
                }else{
                    tmpSTime.replace(0,tmpSTime.length(),sdf.format(c_tmp_begin.getTime())+"-"+sdf.format(c_tmp_end.getTime()));
                }
                count = addWeeksForNullOfNewList( count, tmpLeng,tmpSTime,oldList, newList, c_tmp_begin, c_tmp_end, statisticResult);
            }
            c_begin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return  newList;
    }

函数二:按周补全数据-具体补全操作

 /**
     * 按周补全数据-具体补全操作
     * @param count
     * @param tmpLeng
     * @param tmpSTime
     * @param oldList
     * @param newList
     * @param c_tmp_begin
     * @param c_tmp_end
     * @param statisticResult
     * @return
     */
    private int addWeeksForNullOfNewList(int count,int tmpLeng, StringBuffer tmpSTime,List<StatisticResult> oldList,ArrayList <StatisticResult> newList,Date c_tmp_begin,Date c_tmp_end,StatisticResult statisticResult){
        if(count<tmpLeng){
            if(c_tmp_begin.compareTo(DateUtil.StringToDate(oldList.get(count).getTime(),"yyyy-MM-dd"))<=0&&
                    c_tmp_end.compareTo(DateUtil.StringToDate(oldList.get(count).getTime(),"yyyy-MM-dd"))>=0){
                statisticResult = oldList.get(count);
                statisticResult.setTime(tmpSTime.toString());
                newList.add(statisticResult);
                count++;
            }else{
                statisticResult  =getNewStatisticResult(tmpSTime.toString(),oldList.get(0).getAction());
                newList.add(statisticResult);
            }
        }else{
            statisticResult  =getNewStatisticResult(tmpSTime.toString(),oldList.get(0).getAction());
            newList.add(statisticResult);
        }
        return count;
    }

四、 按月补全数据

 /**
     * 按月补全数据
     * @param oldList
     * @param start
     * @param end
     * @return
     */
    public   List<StatisticResult> addMonthsForNull(List<StatisticResult> oldList,String start,String end){
        Calendar  c_begin = DateUtil.StringtoCalendar(start,"yyyy-MM");
        Calendar   c_end = DateUtil.StringtoCalendar(end,"yyyy-MM");
        SimpleDateFormat  dateFormat  = new SimpleDateFormat("yyyy-MM");
        ArrayList <StatisticResult> newList = new ArrayList<StatisticResult>();
        if(c_begin==null||c_end==null){
            return newList;
        }
        Date c_tmp_begin = c_begin.getTime();
        Date c_tmp_end = c_end.getTime();
        int tmpLeng = oldList.size();
        int count = 0;
        StatisticResult statisticResult = null;
        while(c_begin.compareTo(c_end)<=0){
            c_tmp_begin = c_begin.getTime();
            if(count<tmpLeng){
                if(c_tmp_begin.compareTo(DateUtil.StringToDate(oldList.get(count).getTime(),"yyyy-MM"))<=0&&
                        c_tmp_end.compareTo(DateUtil.StringToDate(oldList.get(count).getTime(),"yyyy-MM"))>=0){
                    statisticResult = oldList.get(count);
                    newList.add(statisticResult);
                    count++;
                }else{
                    statisticResult  =getNewStatisticResult(dateFormat.format(c_tmp_begin),oldList.get(0).getAction());
                    newList.add(statisticResult);
                }
            }else{
                statisticResult  =getNewStatisticResult(dateFormat.format(c_tmp_begin),oldList.get(0).getAction());
                newList.add(statisticResult);
            }
            c_begin.add(Calendar.MONTH, 1);
        }
        return  newList;
    }

参考资料

统计每天的数据 无数据也要显示日期程序解决方法

java得到指定时间段中的所有周,以及每周的每天都是周一和日期是多少?

数据统计第一弹-按时/天/周/月补全某一段时间的数据-Java核心逻辑

参与评论