2014-09-04 10:34:41
2013-08-04 13:42:19
2014-09-04 16:46:49
2010-01-04 12:32:00
2004-04-04 10:34:41
2009-05-14 23:42:19
2014-12-04 06:08:49
2010-01-24 01:32:00
进行倒序排序,取出最大的一个时间
6 个解决方案
#1
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Test13 {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
List<String> list =new ArrayList<String>();
list.add("2014-09-04 10:34:41");
list.add("2013-08-04 13:42:19");
list.add("2014-09-04 16:46:49");
list.add("2010-01-04 12:32:00");
list.add("2004-04-04 10:34:41");
list.add("2009-05-14 23:42:19");
list.add("2014-12-04 06:08:49");
list.add("2010-01-24 01:32:00");
List<String> descList=sortListDesc(list);
System.out.println(descList.toString());
System.out.println("最近的时间为:"+descList.get(0));
}
public static List<String> sortListDesc(List<String> list) throws ParseException{
List<String> retStr=new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<Long,String> map = new TreeMap<Long,String>();
for(int i=0;i<list.size();i++){
String dateStr = list.get(i);
map.put(sdf.parse(dateStr).getTime(), dateStr);
}
Collection<String> coll=map.values();
retStr.addAll(coll);
Collections.reverse(retStr);
return retStr;
}
}
#2
List<String> list =new ArrayList<String>();
list.add("2014-09-04 10:34:41");
list.add("2013-08-04 13:42:19");
list.add("2014-09-04 16:46:49");
list.add("2010-01-04 12:32:00");
list.add("2004-04-04 10:34:41");
list.add("2009-05-14 23:42:19");
list.add("2014-12-04 06:08:49");
list.add("2010-01-24 01:32:00");
Collections.sort(list);
System.out.println(list.get(0));
#3
如果能够确定日期格式都是规整且合理的, 用字符串的排序就可以。 像2楼那样
#4
这是升序,请问下降序是哪个方法啊
#5
这个是源码,可以自己跟踪
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
#6
2楼无法求出最近的时间
#1
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Test13 {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
List<String> list =new ArrayList<String>();
list.add("2014-09-04 10:34:41");
list.add("2013-08-04 13:42:19");
list.add("2014-09-04 16:46:49");
list.add("2010-01-04 12:32:00");
list.add("2004-04-04 10:34:41");
list.add("2009-05-14 23:42:19");
list.add("2014-12-04 06:08:49");
list.add("2010-01-24 01:32:00");
List<String> descList=sortListDesc(list);
System.out.println(descList.toString());
System.out.println("最近的时间为:"+descList.get(0));
}
public static List<String> sortListDesc(List<String> list) throws ParseException{
List<String> retStr=new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<Long,String> map = new TreeMap<Long,String>();
for(int i=0;i<list.size();i++){
String dateStr = list.get(i);
map.put(sdf.parse(dateStr).getTime(), dateStr);
}
Collection<String> coll=map.values();
retStr.addAll(coll);
Collections.reverse(retStr);
return retStr;
}
}
#2
List<String> list =new ArrayList<String>();
list.add("2014-09-04 10:34:41");
list.add("2013-08-04 13:42:19");
list.add("2014-09-04 16:46:49");
list.add("2010-01-04 12:32:00");
list.add("2004-04-04 10:34:41");
list.add("2009-05-14 23:42:19");
list.add("2014-12-04 06:08:49");
list.add("2010-01-24 01:32:00");
Collections.sort(list);
System.out.println(list.get(0));
#3
如果能够确定日期格式都是规整且合理的, 用字符串的排序就可以。 像2楼那样
#4
这是升序,请问下降序是哪个方法啊
#5
这个是源码,可以自己跟踪
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
#6
2楼无法求出最近的时间