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是空值
骑狗啃月亮
9 years, 6 months ago