2016-12-07 51 views

回答

4
Use Comparator to sort by date 
Following code may help you 

class Comp implements Comparator<Date> 
{ 
    public int compare(Date o1, Date o2) { 
     return -(o1.compareTo(o2)); //implement here order you want 
    } 


} 

public static void main(String[] args) 
{ 
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
    String dates[] = { "05-11-2014" , "23-08-2014" , "05-02-2013" }; 
    List<Date> list = new ArrayList<Date>(); 
    for (String string : dates) { 
     try { 
      Date localdate = sdf.parse(string); 
      list.add(localdate); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
    Collections.sort(list); 

    System.out.println(list); 

    Collections.sort(list, new Comp()); 

    System.out.println(list); 
} 
相關問題