list集合中获取最近的时间,以及最近时间与现在的时间差

list集合中获取最近的时间
List list = new ArrayList();
list.add("2012-11-01 12:12:20");
list.add("2012-11-01 12:13:20");
list.add("2012-11-01 12:10:20");
list.add("2012-10-31 12:14:20");
list.add("2012-11-01 11:14:20");

System.out.println(Arrays.toString(list.toArray()));
Date later = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(String date:list){
try{
if(later == null){
later = sdf.parse(date);
}else{
if(later.before(sdf.parse(date))){
later = sdf.parse(date);
}
}
}catch(Exception e){
e.printStackTrace();
}
}

System.out.println(sdf.format(later));
 
  

得出最大时间后,怎么得到现在(此刻)的时间和最大(得到的)的时间的差?
public static void main(String[] args) {
  List list = new ArrayList();
  list.add("2012-11-01 12:12:20");
  list.add("2012-11-01 12:13:20");
  list.add("2012-11-01 12:10:20");
  list.add("2012-10-31 12:13:20");
  list.add("2012-11-01 11:14:20");

System.out.println(Arrays.toString(list.toArray()));
  Date later = null;
  Date prior = null;
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  for (String date : list) {
   try {
    if (later == null) {
     later = sdf.parse(date);
     prior = sdf.parse(date);
    } else {
     if (later.before(sdf.parse(date))) {
      later = sdf.parse(date);
     }
     if(prior.after(sdf.parse(date))) {
      prior = sdf.parse(date);
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  System.out.println("最近时间:"+sdf.format(later));
  System.out.println("最远时间:"+sdf.format(prior));
  //相减
  long time = later.getTime() - prior.getTime();
  String result = "";
  //转成分钟
  time = time/60000; 
  //判断是否超过一天
  if(time/60>=24){
   result = time/60/24 + "天";
  }else{
   if(time/60==0){
    result = time + "分钟";
   }else{
    result = time/60 + "小时";
   }
  }
  System.out.println(result);
 }


你可能感兴趣的:(list,Date)