00000004 2011-10-08 08:21:59
00000001 2011-10-08 13:36:59
00000001 2011-10-09 08:17:59
00000001 2011-10-09 18:31:59
00000001 2011-10-10 08:08:59
00000001 2011-10-10 19:01:59
00000001 2011-10-11 08:12:59
00000001 2011-10-11 17:21:59
00000001 2011-10-11 18:51:59
00000001 2011-10-12 08:56:59
上面是一个List<Employee>,分别为(ID,打卡日期,打卡时间)请问如何比较每天第一次打卡和最后一次打卡时间。
如何统计出只有一次打卡的情况。请大神给点代码

解决方案 »

  1.   

    首先,问题没说清楚,不知道你要对整个数据做以上操作还是对每个员工做以上操作
    其次,如果你的list里的数据是从数据库查出来的,那干嘛不在查询的时候就把这个事情给做了,sql做,又快又方便
      

  2.   

    先获取每个员工的打卡记录 遍历list的时候 , 放在hashmap里面  map<String ,List<String>>的key为打卡日期 ,value为该员工这一天的所有打卡时间 
      

  3.   

    Employees.GroupBy(value => value.ID).SelectMany(values => values.GroupBy(value => value.打卡日期).Select(times => new { ID = values.Key, 打卡日期 = times.Key, 第一次时间 = times.Min(time => time.打卡时间), 最后时间 = times.Max(time => time.打卡时间) }))
      

  4.   

    下班时看到,匆忙写了些,没写注释,楼主可以参考下..
    现在写完才发现一个问题,"如何统计出只有一次打卡的情况" 是指一个人一天只有一次打卡 还是一个人只有一天打卡 ? 我的代码是按"一个人一天只有一次打卡处理的",下面是我的代码:
    /*
     * To change this template, choose Tools | Templates and open the template in
     * the editor.
     */
    package com.csdn.question;import java.io.*;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;/**
     * 
     * @author karl
     */
    public class QueryPunchCard {
        
        public static void init( File src ) throws FileNotFoundException, IOException{
            BufferedReader reader = new BufferedReader(new FileReader(src));
            
            String temp = null;
            
            while( (temp = reader.readLine()) != null && !temp.equals("")){
                String[] subs = temp.split(" ");
                String id = subs[0];
                String date = subs[1];
                String time = subs[2];
                
                Employee emplyee = Employee.employees.get(id);
                if( emplyee == null ){
                    emplyee = new Employee(id);
                    Employee.employees.put(id,emplyee);
                }
                
                PunchCardRecord record = emplyee.records.get(date);
                if( record == null ){
                    record = new PunchCardRecord(date);
                    emplyee.records.put(date,record);
                }
                
                String[] time_subs = time.split(":");
                String hour = time_subs[0];
                int hour_int = Integer.parseInt(hour);
                
                if( hour_int < 12 ){
                   record.mor_time = time; 
                }else {
                   record.eve_time = time; 
                }
            }
            
        }
        
        public static void compareMor_timeAndEve_Time(){
            
            Set<String> ids = Employee.employees.keySet();
            if( ids.size() == 0 ) return;
            
            for( String id : ids ){
               Employee employee =   Employee.employees.get(id);
               employee.show();
            }
            
        }
        
        
        public static void onlyone(){
            Set<String> ids = Employee.employees.keySet();
            if( ids.size() == 0 ) return;
            
            for( String id : ids ){
               Employee employee =   Employee.employees.get(id);
               employee.onlyone();
            }
        }
        
        
        
        public static void main( String[] args ) throws FileNotFoundException, IOException{
            init( new File("H:/employ.txt"));
            //compareMor_timeAndEve_Time();
            //onlyone();
        }
        
        
    }class Employee {
        
        static Map<String,Employee> employees = new HashMap<String,Employee>();
        
        String id;
        Map<String,PunchCardRecord> records = new HashMap<String,PunchCardRecord>();
        
        Employee(){}
        
        Employee( String id){
            this.id = id;
        }
        
        public void show(){
            Set<String> dates = records.keySet();
            if( dates.size() == 0 ){
                return ;
            }
            
            for( String date : dates ){
                PunchCardRecord record = records.get(date);
                System.out.println(" ID: "+id+" Date: "+date+" 第一次打卡: "
                                    +record.mor_time+" 最后一次打卡: "+record.eve_time);
            }
           
        }
        
        public void onlyone(){
            Set<String> dates = records.keySet();
            if( dates.size() == 0 ){
                return ;
            }
            
            for( String date : dates ){
                PunchCardRecord record = records.get(date);
                if( ( record.mor_time == null && record.eve_time != null ) 
                        ||(record.mor_time != null && record.eve_time == null) ){
                   System.out.println(" ID: "+id+" Date: "+date+" 打卡一次 "); 
                }
                
            }
        }
        
        
        
    }
    class PunchCardRecord{
       
        String date;
        String mor_time;
        String eve_time;
        
        PunchCardRecord(){}
        
        PunchCardRecord(String date){
            this.date = date;
       
        }
        
        
        
    }测试结果 :
      1.比较每天第一次打卡和最后一次打卡时间
     ID: 00000001 Date: 2011-10-12 第一次打卡: 08:56:59 最后一次打卡: null
     ID: 00000001 Date: 2011-10-08 第一次打卡: null 最后一次打卡: 13:36:59
     ID: 00000001 Date: 2011-10-09 第一次打卡: 08:17:59 最后一次打卡: 18:31:59
     ID: 00000001 Date: 2011-10-11 第一次打卡: 08:12:59 最后一次打卡: 18:51:59
     ID: 00000001 Date: 2011-10-10 第一次打卡: 08:08:59 最后一次打卡: 19:01:59
     ID: 00000004 Date: 2011-10-08 第一次打卡: 08:21:59 最后一次打卡: null
      2.统计出只有一次打卡的情况
     ID: 00000001 Date: 2011-10-12 打卡一次 
     ID: 00000001 Date: 2011-10-08 打卡一次 
     ID: 00000004 Date: 2011-10-08 打卡一次