Java ArrayList


数据如下


 [click start=2014-07-14 21:00:53, video play over=2014-07-14 21:02:24]
[click start=2014-06-20 15:36:19, click start=2014-06-20 15:36:19, click start=2014-06-20 15:36:19, video play over=2014-06-20 15:36:46]

部分代码如下:


 Map<String,ArrayList> idAndInfo = new HashMap<String,ArrayList>();
//act1和time1是传递的参数
Startplay startplay = new Startplay(act1,time1);
ArrayList startplays = idAndInfo.get(id);
if (startplays == null){
    startplays = new ArrayList();
    startplays.add(startplay);
    idAndInfo.put(id, startplays);
    }else{
            startplays.add(startplay);
        }
    Set<String> keys = idAndInfo.keySet();
    Iterator<String> iter = keys.iterator();
    //int i=0;
    while(iter.hasNext()){
        String str = iter.next();
        ArrayList actandtime = idAndInfo.get(str);
        //输出的结果如上所示的数据
        System.out.println(actandtime);
        int location = actandtime.indexOf("click");
        System.out.println(location);
        if (actandtime.contains("click start") && actandtime.contains("video play over")){
        //调用方法
        long watch_length= Simpledateformat(actandtime.indexOf("click start"),actandtime.indexOf("video play over"));
        System.out.println("watch_length:"+ watch_length + "s");
        }
    }
    //方法
    public long Simpledateformat(String s1, String s2){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null; 
        Date date2 = null; 
        try { 
            date1 = sdf.parse(s1); 
            date2 = sdf.parse(s2); 
            } 
        catch (ParseException ex) { 

        } 
        long miliS = date2.getTime() - date1.getTime(); //相差的毫秒数
        long sec = miliS / 1000; //结果就是10
        return sec;
    }

通过调用方法实现,但结果location返回的是-1,watch_length是空值

java arraylist

骑狗啃月亮 9 years, 7 months ago

actandtime.indexOf("click start") 获取"click start"字符串出现的位置
你的目的是获取
click start=2014-07-14 21:00:53
中 2014-07-14 21:00:53

现有的代码无法达到你的目的
Simpledateformat 方法有try/catch中有异常,你把异常print出来就知道怎么回事了

Ming, answered 9 years, 7 months ago

Your Answer