qq_37043678
2017-06-13 08:46
采纳率: 100%
浏览 1.8k
已采纳

获取CSV文件(考勤信息)内容放入集合,怎么获取集合中的每人每天的最早和最晚时间。求大神解决。

List dkinfos = new ArrayList();//保存读取csv得到的数据

Map> map = new HashMap>();//分组统计的map集合

public void generic(){
String sbip = null;

List mapDkinfos = new ArrayList();//map集合中每组ip的对应的对象集合

BufferedReader reader = null;

//读写csv数据到list(dkinfos)集合中

try {

reader = new BufferedReader(new FileReader("C:\Users\Lollipop\Desktop\考勤信息.CSV"));//换成你的文件名

String line = null;

while((line=reader.readLine())!=null){

String data[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分

XXattendance dkinfo = new XXattendance();
dkinfo.setName(data[1]);
dkinfo.setN_id(data[2]);

dkinfo.setDate(data[3]);

dkinfos.add(dkinfo);

// System.out.println(data[0]+","+data[1]);

// System.out.println(dkinfo.getName()+dkinfo.getN_id()+dkinfo.getDate());
}
//遍历集合,将数据分组存入map集合

for(XXattendance dkinfo : dkinfos){

sbip = dkinfo.getN_id();

sbip = dkinfo.getDate();
mapDkinfos = map.get(sbip);//获取对应sbip的集合,第一次获取时为空

if(mapDkinfos == null){//这里如果不进行为空判断,会报空指针异常

mapDkinfos = new ArrayList();

}

mapDkinfos.add(dkinfo);//将新添加的对象加入到对应的sbip的对象集合

map.put(sbip, mapDkinfos); //将每次新添加对象后的对象集合存入对应map中

}

Iterator iter = dkinfos.iterator();
//遍历map集合

for (Map.Entry> entry : map.entrySet()) {

// System.out.println("考勤号: " + entry.getKey() + "打卡次数:" + entry.getValue().size());

}

} catch (Exception e) {  
    e.printStackTrace();  
} finally{  
    try {  
        reader.close();  
        dkinfos.clear();  
        map.clear();  
        mapDkinfos.clear();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
}  

SCV数据:
"部门","姓名","考勤号码","日期时间","机器号","编号","比对方式","卡号"
,"王","005","2017/5/2 9:06:13","1","","指纹",""
,"王","005","2017/5/2 10:35:04","1","","指纹",""
,"王","005","2017/5/2 19:21:28","1","","指纹",""
,"王","005","2017/5/2 19:22:43","1","","指纹",""
,"王","005","2017/5/3 13:58:49","1","","密码",""
,"王","005","2017/5/3 14:01:46","1","","密码",""
,"王","005","2017/5/3 14:10:11","1","","密码",""
,"王","005","2017/5/3 16:58:06","1","","指纹",""
,"王","005","2017/5/4 8:57:29","1","","指纹",""
,"王","005","2017/5/4 9:53:30","1","","密码",""
,"王","005","2017/5/4 11:03:16","1","","指纹",""
,"王","005","2017/5/4 12:29:13","1","","指纹",""
,"王","005","2017/5/4 13:58:32","1","","指纹",""
,"王","005","2017/5/4 14:24:46","1","","指纹",""
,"王","005","2017/5/4 14:49:02","1","","指纹",""
,"王","005","2017/5/4 16:22:15","1","","指纹",""
,"王","005","2017/5/4 18:10:31","1","","指纹",""
,"王","005","2017/5/5 8:53:24","1","","指纹",""
,"王","005","2017/5/5 9:21:41","1","","指纹",""
,"王","005","2017/5/5 11:30:13","1","","密码",""
,"王","005","2017/5/5 12:59:00","1","","指纹",""
,"王","005","2017/5/5 13:01:05","1","","密码",""
,"王","005","2017/5/5 13:48:33","1","","指纹",""
,"王","005","2017/5/5 15:38:59","1","","指纹",""
,"王","005","2017/5/5 18:03:22","1","","指纹",""
,"王","005","2017/5/8 10:11:59","1","","指纹",""
,"王","005","2017/5/8 10:36:55","1","","指纹",""
,"王","005","2017/5/8 11:34:26","1","","指纹",""
,"王","005","2017/5/8 11:46:22","1","","指纹",""
,"王","005","2017/5/8 13:01:05","1","","指纹",""
,"王","005","2017/5/8 13:42:16","1","","指纹",""
,"王","005","2017/5/8 15:41:08","1","","指纹",""
,"王","005","2017/5/8 15:44:01","1","","指纹",""
,"王","005","2017/5/8 18:06:13","1","","指纹",""
,"王","005","2017/5/8 18:10:54","1","","指纹",""
,"王","005","2017/5/9 9:02:09","1","","指纹",""
,"王","005","2017/5/9 10:45:04","1","","指纹",""
,"王","005","2017/5/9 15:54:49","1","","指纹",""
,"王","005","2017/5/9 16:21:58","1","","指纹",""
,"王","005","2017/5/12 8:43:06","1","","指纹",""
,"王","005","2017/5/15 8:58:31","1","","指纹",""
,"王","005","2017/5/15 9:07:21","1","","指纹",""
,"王","005","2017/5/15 13:47:58","1","","指纹",""
,"李","007","2017/5/3 18:15:51","1","","指纹",""
,"李","007","2017/5/4 8:46:05","1","","指纹",""
,"李","007","2017/5/4 8:59:47","1","","指纹",""
,"李","007","2017/5/4 11:13:44","1","","指纹",""
,"李","007","2017/5/4 12:08:21","1","","指纹",""
,"李","007","2017/5/4 13:27:26","1","","指纹",""
,"李","007","2017/5/4 15:32:24","1","","指纹",""
,"李","007","2017/5/4 16:50:40","1","","指纹",""
,"李","007","2017/5/4 18:10:17","1","","指纹",""
,"李","007","2017/5/5 8:45:16","1","","指纹",""
,"李","007","2017/5/5 8:55:33","1","","指纹",""
,"李","007","2017/5/5 8:56:18","1","","指纹",""
,"李","007","2017/5/5 16:38:41","1","","指纹",""
,"李","007","2017/5/5 18:03:41","1","","指纹",""
,"李","007","2017/5/8 8:49:04","1","","指纹",""
,"李","007","2017/5/8 8:50:14","1","","指纹",""
,"李","007","2017/5/8 10:03:45","1","","指纹",""
,"李","007","2017/5/8 12:18:39","1","","指纹",""
,"李","007","2017/5/8 12:27:55","1","","指纹",""
,"李","007","2017/5/8 14:02:19","1","","指纹",""
,"李","007","2017/5/8 14:33:45","1","","指纹",""
,"李","007","2017/5/8 18:06:19","1","","指纹",""
,"李","007","2017/5/8 18:18:26","1","","指纹",""
,"李","007","2017/5/9 8:48:39","1","","指纹",""
,"李","007","2017/5/9 8:49:53","1","","指纹",""
,"李","007","2017/5/9 13:33:35","1","","指纹",""
,"李","007","2017/5/9 15:00:58","1","","指纹",""
,"李","007","2017/5/9 17:40:10","1","","指纹",""
,"李","007","2017/5/9 18:07:28","1","","指纹",""
,"李","007","2017/5/12 8:55:34","1","","指纹",""
,"李","007","2017/5/12 8:58:09","1","","指纹",""
,"李","007","2017/5/12 11:11:20","1","","指纹",""
,"李","007","2017/5/12 15:09:12","1","","指纹",""
,"李","007","2017/5/12 16:27:09","1","","指纹",""
,"李","007","2017/5/12 17:56:23","1","","指纹",""
,"李","007","2017/5/12 18:10:22","1","","指纹",""
,"李","007","2017/5/15 8:50:59","1","","指纹",""
,"李","007","2017/5/15 9:06:47","1","","指纹",""
,"张","008","2017/5/2 9:13:02","1","","指纹",""
,"张","008","2017/5/2 16:28:27","1","","指纹",""
,"张","008","2017/5/2 17:38:45","1","","指纹",""
,"张","008","2017/5/3 9:04:40","1","","指纹",""
,"张","008","2017/5/3 9:08:44","1","","指纹",""
,"张","008","2017/5/3 13:56:29","1","","指纹",""
,"张","008","2017/5/4 10:40:16","1","","指纹",""
,"张","008","2017/5/4 11:56:22","1","","指纹",""
,"张","008","2017/5/4 13:17:10","1","","指纹",""
,"张","008","2017/5/4 13:34:48","1","","密码",""
,"张","008","2017/5/4 15:34:28","1","","密码",""
,"张","008","2017/5/4 15:49:44","1","","指纹",""
,"张","008","2017/5/5 10:58:58","1","","指纹",""
,"张","008","2017/5/5 12:34:57","1","","密码",""
,"张","008","2017/5/5 16:45:05","1","","指纹",""
,"张","008","2017/5/8 10:17:23","1","","指纹",""
,"张","008","2017/5/8 10:23:07","1","","指纹",""
,"张","008","2017/5/8 12:10:50","1","","指纹",""
,"张","008","2017/5/8 15:10:49","1","","指纹",""
,"张","008","2017/5/9 10:13:12","1","","指纹",""
,"张","008","2017/5/9 11:51:06","1","","指纹",""
,"张","008","2017/5/9 12:19:36","1","","指纹",""
,"张","008","2017/5/9 13:30:50","1","","指纹",""
,"张","008","2017/5/11 10:13:07","1","","指纹",""
,"张","008","2017/5/11 11:26:39","1","","指纹",""
,"张","008","2017/5/11 12:04:37","1","","指纹",""
,"张","008","2017/5/11 13:56:43","1","","指纹",""
,"张","008","2017/5/15 9:19:06","1","","指纹",""
,"张","008","2017/5/15 12:07:44","1","","指纹",""
,"张","008","2017/5/15 13:14:25","1","","密码",""
,"于","009","2017/5/2 9:10:01","1","","指纹",""
,"于","009","2017/5/2 17:58:15","1","","指纹",""
,"于","009","2017/5/3 9:07:14","1","","指纹",""
,"于","009","2017/5/3 18:15:26","1","","指纹",""
,"于","009","2017/5/4 8:58:18","1","","指纹",""
,"于","009","2017/5/4 9:51:00","1","","指纹",""
,"于","009","2017/5/4 13:56:46","1","","指纹",""
,"于","009","2017/5/4 14:47:43","1","","指纹",""
,"于","009","2017/5/4 15:51:32","1","","指纹",""
,"于","009","2017/5/4 16:49:36","1","","指纹",""
,"于","009","2017/5/4 18:10:11","1","","指纹",""
,"于","009","2017/5/5 8:46:30","1","","指纹",""
,"于","009","2017/5/5 8:59:37","1","","指纹",""
,"于","009","2017/5/5 9:25:21","1","","指纹",""
,"于","009","2017/5/5 9:54:00","1","","指纹",""
,"于","009","2017/5/5 10:31:46","1","","指纹",""
,"于","009","2017/5/5 13:38:04","1","","指纹",""
,"于","009","2017/5/5 14:53:26","1","","指纹",""
,"于","009","2017/5/5 16:39:31","1","","指纹",""
,"于","009","2017/5/5 17:49:45","1","","指纹",""
,"于","009","2017/5/5 18:03:37","1","","指纹",""
,"于","009","2017/5/8 8:48:25","1","","指纹",""
,"于","009","2017/5/8 9:48:53","1","","指纹",""
,"于","009","2017/5/8 12:42:48","1","","指纹",""
,"于","009","2017/5/8 14:13:43","1","","指纹",""
,"于","009","2017/5/8 16:10:01","1","","指纹",""
,"于","009","2017/5/8 16:57:31","1","","指纹",""
,"于","009","2017/5/8 18:26:59","1","","指纹",""
,"于","009","2017/5/8 18:57:02","1","","指纹",""
,"于","009","2017/5/8 18:58:06","1","","指纹",""
,"于","009","2017/5/9 8:42:26","1","","指纹",""
,"于","009","2017/5/9 9:34:07","1","","指纹",""
,"于","009","2017/5/9 11:46:12","1","","指纹",""
,"于","009","2017/5/9 13:47:02","1","","指纹",""
,"于","009","2017/5/9 17:25:17","1","","指纹",""
,"于","009","2017/5/9 18:05:02","1","","指纹",""
,"于","009","2017/5/10 8:46:50","1","","指纹",""
,"于","009","2017/5/10 10:14:49","1","","指纹",""
,"于","009","2017/5/10 10:37:15","1","","指纹",""
,"于","009","2017/5/10 11:35:05","1","","指纹",""
,"于","009","2017/5/10 13:34:00","1","","指纹",""
,"于","009","2017/5/10 15:28:08","1","","指纹",""
,"于","009","2017/5/10 16:13:17","1","","指纹",""
,"于","009","2017/5/10 18:11:05","1","","指纹",""
,"于","009","2017/5/11 8:50:41","1","","指纹",""
,"于","009","2017/5/11 9:57:43","1","","指纹",""
,"于","009","2017/5/11 10:18:26","1","","指纹",""
,"于","009","2017/5/11 11:25:47","1","","指纹",""
,"于","009","2017/5/11 13:49:23","1","","指纹",""
,"于","009","2017/5/11 14:02:04","1","","指纹",""
,"于","009","2017/5/11 16:58:33","1","","指纹",""
,"于","009","2017/5/11 18:23:28","1","","指纹",""
,"于","009","2017/5/12 8:49:37","1","","指纹",""
,"于","009","2017/5/12 11:27:42","1","","指纹",""
,"于","009","2017/5/12 13:47:13","1","","指纹",""
,"于","009","2017/5/12 14:10:01","1","","指纹",""
,"于","009","2017/5/12 15:52:46","1","","指纹",""
,"于","009","2017/5/12 17:30:29","1","","指纹",""
,"于","009","2017/5/12 18:09:34","1","","指纹",""
,"于","009","2017/5/15 8:50:10","1","","指纹",""
,"于","009","2017/5/15 13:38:37","1","","指纹",""

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 冷咖啡15 2017-06-14 04:50
    已采纳

    package com.leeh.util;

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class HandleCVS {
    List dkinfos = new ArrayList();// 保存读取csv得到的数据

    Map<String, List<XXattendance>> map = new HashMap<String, List<XXattendance>>();// 分组统计的map集合
    
    public void generic() {
        String sbip = null;
    
        List<XXattendance> mapDkinfos = null;// map集合中每组ip的对应的对象集合
    
        BufferedReader reader = null;
    
        // 读写csv数据到list(dkinfos)集合中
    
        try {
    
            reader = new BufferedReader(new FileReader("E:\\Java\\JavaWeb\\SS\\src\\main\\resources\\ss.cvs"));// 换成你的文件名
    
            reader.readLine();//去掉标题
            String line = null;
            while ((line = reader.readLine()) != null) {
    
                String data[] = line.split(",");// CSV格式文件为逗号分隔符文件,这里根据逗号切分
    
                XXattendance dkinfo = new XXattendance();
                dkinfo.setName(data[1]);
                dkinfo.setN_id(data[2]);
    
                dkinfo.setDate(data[3]);
    
                dkinfos.add(dkinfo);
    
                // System.out.println(data[0]+","+data[1]);
    
                // System.out.println(dkinfo.getName()+dkinfo.getN_id()+dkinfo.getDate());
            }
            // 遍历集合,将数据分组存入map集合
    
            for (XXattendance dkinfo : dkinfos) {
    
                sbip = dkinfo.getN_id();
    
                //sbip = dkinfo.getDate();
                mapDkinfos = map.get(sbip);// 获取对应sbip的集合,第一次获取时为空
    
                if (mapDkinfos == null) {// 这里如果不进行为空判断,会报空指针异常
    
                    mapDkinfos = new ArrayList<XXattendance>();
    
                }
    
                mapDkinfos.add(dkinfo);// 将新添加的对象加入到对应的sbip的对象集合
    
                map.put(sbip, mapDkinfos); // 将每次新添加对象后的对象集合存入对应map中
    
            }
    
            System.out.println("\n\n");
    
    
            for (Map.Entry<String, List<XXattendance>> entry : map.entrySet()) {
                mapDkinfos = entry.getValue();
                mapDkinfos.sort(new XXattendance());
                XXattendance xx = mapDkinfos.get(0);
                System.out.print("考勤号:" + xx.getN_id() + "  " + xx.getDate().split(" ")[0] + "\"\t最早考勤时间:\"" 
                + xx.getDate().split(" ")[1] + "\t最晚考勤时间:\"");
                for (XXattendance attendance : mapDkinfos) {
                    if (!xx.isSameDay(attendance)) {
                        System.out.println(xx.getDate().split(" ")[1]);
                        System.out.print("考勤号:" + attendance.getN_id() + "  " + attendance.getDate().split(" ")[0] + "\"\t最早考勤时间:\"" 
                                + attendance.getDate().split(" ")[1] + "\t最晚考勤时间:\"");
                    }
                    xx = attendance;
                }
                System.out.println(xx.getDate().split(" ")[1] + "\n\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
                dkinfos.clear();
                map.clear();
                mapDkinfos.clear();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        HandleCVS handleCVS = new HandleCVS();
        handleCVS.generic();
    }
    

    }

     这个是根据你的代码改的  
    
     下面这个类是根据你的代码猜测的pojo
    
    

    package com.leeh.util;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Comparator;
    import java.util.Date;

    public class XXattendance implements Comparator {
    private String name;
    private String n_id;
    private String date;

    public XXattendance() {
    
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getN_id() {
        return n_id;
    }
    
    public void setN_id(String n_id) {
        this.n_id = n_id;
    }
    
    public String getDate() {
        return date;
    }
    
    public void setDate(String date) {
        this.date = date;
    }
    
    /**
     * 判断是否为同一天
     * 
     * @param attendance
     * @return
     */
    public boolean isSameDay(XXattendance attendance) {
    
        return this.date.split(" ")[0].equals(attendance.getDate().split(" ")[0]);
    }
    
    @Override
    public int compare(XXattendance attendance1, XXattendance attendance2) {
        Date date1 = null;
        Date date2 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("\"yyyy/MM/dd HH:mm:ss\"");
        try {
            date1 = formatter.parse(attendance1.getDate());
            date2 = formatter.parse(attendance2.getDate());
        } catch (ParseException e) {
    
            e.printStackTrace();
        }
        return (int)(date1.getTime() - date2.getTime());
    }
    

    }

    打赏 评论
  • 冷咖啡15 2017-06-13 08:44

    请把数据放上来啊,没有数据怎么测试

    打赏 评论
  • 庄粟 2017-06-14 03:44

    根据数据结构来:

    目标是将数据转换成个人以天为数据的集合对象 data:map{'编号':map{'yyy-mm-dd':[当天时间集]}};

    先做一次数据的遍历:
    获取编号,
    以编号去data中拿数据没有数据就新建,
    有就截取日期年月日为KEY同上,
    最后把日期放入集合中。

    转换成功后,获取data的keyset
    做三层循环,最内层声明两个变量bigNum,smNum
    对比出当天最大最小值,就可以保存起来了

    打赏 评论
  • sb3day 2017-07-01 04:35
    打赏 评论

相关推荐 更多相似问题