博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将一段时间内没有数据的点用0填充
阅读量:7120 次
发布时间:2019-06-28

本文共 4971 字,大约阅读时间需要 16 分钟。

hot3.png

需求如下

2017-10-01,0,852017-10-01,1,52017-10-01,2,192017-10-01,4,12017-10-01,5,322017-10-01,6,382017-10-01,8,1332017-10-01,9,1072017-10-01,10,372017-10-01,12,532017-10-01,13,1072017-10-01,14,672017-10-01,15,1072017-10-01,16,462017-10-01,17,692017-10-01,18,702017-10-01,19,222017-10-01,21,1352017-10-01,22,193

结果如下

2017-10-01,85,5,19,0,1,32,38,0,133,107,37,0,53,107,67,107,46,69,70,22,0,135,193,0

读文件

@Test    public void  readFileTest() {        File file = new File("D:/reply.log");        List listdate=new ArrayList();        for (int i=37;i>0;i--){    //控制你要上传的数据的日期,自定义            listdate.add(getNextDay(i));        }        List list=new ArrayList();        Map map=new LinkedHashMap();        try {            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件            String s = null;            String stdate="2017-10-01";            int index=0;            while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行                String[] strArray = s.split(","); //拆分字符为"," ,然后把结果交给数组strArray                if (compare_date(listdate.get(index).toString(),strArray[0])){   //将数据通过日期分组                    map.put(strArray[1],strArray[2]);                }else {                    list.add(map);                    index=index+1;                    stdate=strArray[0];                    map=new LinkedHashMap();                    map.put(strArray[1],strArray[2]);                }            }            list.add(map);            br.close();        } catch (Exception e) {            e.printStackTrace();        }        con(list,listdate);//检查没有时间点的位置用0填充    }

数据填零

/**     * 源数据集合,日期数据集合     * @param list     * @param listdate     */    private void con(List list ,List listdate){        Map resultMap=new LinkedHashMap();        int n=0;//控制代表取日期        for(Map map:list){            Iterator iter = map.entrySet().iterator();            List resultList=new ArrayList();            int index =0; //控制代表0—23个时间点            while (iter.hasNext()) {                Map.Entry entry = (Map.Entry) iter.next();                String keys =(String) entry.getKey();                int key=Integer.parseInt(keys);                if (index==key){                    resultList.add(entry.getValue());                    index++;                }else {                    while (key!=index){//防止连续几天都没有数据的情况 2                        resultList.add(0);                        index++;                    }                    resultList.add(entry.getValue());                    index++;                }            }            for (;index<24;){   //判断遍历完了之后几天是否有数据  没有用0填充                resultList.add(0);                index++;            }            resultMap.put(listdate.get(n),resultList);            n++;        }        System.out.println(resultMap);        converKeyword(resultMap);    }

写文件

public static void converKeyword(Map tempList){        File file = new File("D:/result_reply_new.txt");        FileWriter fw = null;        BufferedWriter writer = null;        try {            fw = new FileWriter(file);            writer = new BufferedWriter(fw);            Iterator iter = tempList.entrySet().iterator();            while (iter.hasNext()) {                Map.Entry entry = (Map.Entry) iter.next();                String keys =(String) entry.getKey();                List list=(List) entry.getValue();                for (int i=0;i<24;i++){                    keys=keys+","+list.get(i);                }                writer.write(keys);                writer.newLine();//换行            }            writer.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();        }catch (IOException e) {            e.printStackTrace();        }finally{            try {                writer.close();                fw.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

工具如期判断

/**     * 判断两个日期是否相等     * @param DATE1     * @param DATE2     * @return     */    public static boolean compare_date(String DATE1, String DATE2) {        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");        try {            Date dt1 = df.parse(DATE1);            Date dt2 = df.parse(DATE2);            if(dt1.getTime()==dt2.getTime()){                return true;            }else {                return false;            }        } catch (Exception exception) {            exception.printStackTrace();        }        return false;    }

工具获得自定日期

/**     * 获得指定日期     * @param index     * @return     */    public static String getNextDay(int index) {        Date date=new Date();        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.add(Calendar.DAY_OF_MONTH, -index);        date = calendar.getTime();        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式        String currentDate=df.format(date);        return currentDate;    }

转载于:https://my.oschina.net/lcyanxi/blog/1563725

你可能感兴趣的文章
优化体系结构 - 算法外置优化计算结构
查看>>
解决vue报错Failed to mount component
查看>>
活学活用! 用Local Storage实现多人聊天室
查看>>
炫酷粒子表白,双十一脱单靠它了!
查看>>
Javascript--常用方法
查看>>
[译]迁移到新的 React Context Api
查看>>
线程池你真不来了解一下吗?
查看>>
【跃迁之路】【424天】程序员高效学习方法论探索系列(实验阶段181-2018.04.05)...
查看>>
Angular学习笔记之集成三方UI框架、控件
查看>>
解决“有边框的子元素宽度设定绝对值后,缩放浏览器会错位”的两种方法
查看>>
angular 基于ng-messages的验证
查看>>
三、取get
查看>>
利用PHP实现常用的数据结构之写在前面(小白系列文章一)
查看>>
使用asprise进行图片验证码识别
查看>>
77% 的网站使用了至少有 1 个漏洞的 JavaScript 库
查看>>
一个可以提高开发效率的Git命令-- Cherry-Pick
查看>>
.NET Core 3.0中的数据库驱动框架System.Data
查看>>
数据库设计中的9大常见错误
查看>>
柔性自动化在物流的应用及探索
查看>>
微软最具价值技术专家:我的16年软件开发经验总结
查看>>