Get the latest time from the list collection, and the difference between the latest time and the current time

Get the latest time from the list collection






















}

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

After getting the maximum time, how to get the difference between the current (now) time and the maximum (obtained) time?
public static void main(String[] args) {
  List<String> list = new ArrayList<String>();
  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);
 }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326091635&siteId=291194637